Seite 1 von 1

Mathematica aus Python benutzen

Verfasst: Montag 30. August 2021, 12:10
von Kobra23759
Hallo,
ist jemanden bekannt, ob und wie man Mathematica aus Python heraus verwenden kann.
Z. Bsp: Kreisgleichung in Mathematica lösen durch Aufruf aus Python
Mathematica:
x=-8; c=-10; d=5; r=15;
NSolve[(x-c)²+y-d)²==r²,{y},Reals]
die zwei von Mathematica errechneten y-Werte in Python weiterverwenden
Danke

Re: Mathematica aus Python benutzen

Verfasst: Montag 30. August 2021, 12:56
von rogerb
@Kobra23759,

ich weiß leider nicht wie man sich aus python mit Mathematika verbindet. Aber es gibt gleich zwei Möglichkeiten in Python selbst. EInmal mit sympy.solvers und mit numpy.roots.
Die erstere gibt eine symbolische und numerische Lösung aus, die zweite gibt eine numerische Lösung aus.
Für numpy.root muss man die Gleichung in die Polynomschreibweise umwandeln.

sympy.solvers:
https://docs.sympy.org/latest/modules/s ... ml#solvers

numpy.roots:
https://numpy.org/doc/stable/reference/ ... umpy-roots

Code: Alles auswählen

from sympy.solvers import solve
from sympy import Symbol


y = Symbol("y")

x = -8
c = -10
d = 5
r = 15
solutions = solve((x - c) ** 2 + (y - d) ** 2 - r ** 2, y)

print(solutions)
print([s.evalf() for s in solutions])

"""
Ausgabe:
[5 - sqrt(221), 5 + sqrt(221)]
[-9.86606874731851, 19.8660687473185]
"""

Code: Alles auswählen

import numpy as np

x = -8
c = -10
d = 5
r = 15

solutions = np.roots([1, -2 * d, d ** 2 - r ** 2 + (x - c) ** 2])
print(solutions)

"""
Ausgabe:
[19.86606875 -9.86606875]
"""

Re: Mathematica aus Python benutzen

Verfasst: Montag 30. August 2021, 13:57
von rogerb
Ich sehe gerade, Wolfram Alpha hat wohl eine API. Meinst du das?
https://reference.wolfram.com/language/ ... ogram.html

Re: Mathematica aus Python benutzen

Verfasst: Montag 30. August 2021, 15:23
von Kobra23759
Danke für Deine Bemühungen.
Bei der Wolfram Alpha-API benutzt man das Internet.
Ich würde die Gleichung gern lokal lösen
Gruß

Re: Mathematica aus Python benutzen

Verfasst: Montag 30. August 2021, 16:26
von __deets__