Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
152 views
in Technique[技术] by (71.8m points)

How can I convert the following Mathematica code into sympy?

I am trying to convert some Mathematica code into python and I didn't know how to handle the following function. Is there a way to go about is using sympy or other python module? TPUV, AM1UV and GUV are vectors computed by other accompanying functions.

perpUnitVector[]:=Module[
{sol,a3,b3,c3,a4,b4,c4},
sol=Quiet[Solve[
{a3,b3,c3}.TPUV==0&&  
{a3,b3,c3}.AM1UV==0&&
{a3,b3,c3}.{a3,b3,c3}==1&&
Sign[Cross[-GUV,{a3,b3,c3}].Cross[{1,0,0},-GUV]]>0,
{a3,b3,c3},Reals]];

{a4,b4,c4}=Flatten[{a3,b3,c3}/.sol[[1]],1]
]

Test case: With TPU = {1,3,4}, AM1UV = {4, 5, 6}, GUV = {1,1,1}, Mathematica gives a4 = -0.408248, b4 = 0.816497, and c4 = -0.408248.

my sympy attempt is here but doesn't seem to work

import numpy as np
import sympy as sym

GUV = [1.0, 1.0, 1.0];
AM1UV = [4.0, 5.0, 6.0];
TPUV = [1.0, 2.0, 3.0];

a3,b3,c3,a4,b4,c4 = sym.symbols('a3 b3 c3 a4 b4 c4')

sol = sym.solveset(np.dot([a3,b3,c3],TPUV),
            np.dot([a3,b3,c3],AM1UV),
            np.dot([a3,b3,c3],[a3,b3,c3])-1,
            np.sign(np.dot(np.cross(-GUV,[a3,b3,c3]),
            np.cross([1,0,0],-GUV)))>0,
            [a3,b3,c3],S.Reals)
            
question from:https://stackoverflow.com/questions/65857160/how-can-i-convert-the-following-mathematica-code-into-sympy

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...