/* * MathPlanner 3.2 - Mathematical design tool. * Copyright(C) 2003 Jarmo Nikkanen * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation. * * You should have received a copy of the GNU General Public License with this program. * */ #include "MathHeaders.h" #include math_node mpl_neg(math_node a) { if (a.type==MN_REAL) a.mpl.m.R=-a.mpl.m.R; if (a.type==MN_COMPLEX) a.mpl.m.R=-a.mpl.m.R, a.mpl.m.I=-a.mpl.m.I; if (a.type==MN_VECTOR) a.mpl.m.I=-a.mpl.m.I, a.mpl.m.J=-a.mpl.m.J, a.mpl.m.K=-a.mpl.m.K; return(a); } math_node mpl_add(math_node a,math_node b) { math_node ans; ans.type=MN_NULL; if (a.type==MN_REAL) { if (b.type==MN_REAL) ans.mpl.m.R=a.mpl.m.R+b.mpl.m.R, ans.type=MN_REAL; else if (b.type==MN_COMPLEX) ans.mpl.m.R=a.mpl.m.R+b.mpl.m.R, ans.mpl.m.I=b.mpl.m.I, ans.type=MN_COMPLEX; } else if (a.type==MN_COMPLEX) { if (b.type==MN_REAL) ans.mpl.m.R=a.mpl.m.R+b.mpl.m.R, ans.mpl.m.I=a.mpl.m.I, ans.type=MN_COMPLEX; else if (b.type==MN_COMPLEX) ans.mpl.m.R=a.mpl.m.R+b.mpl.m.R, ans.mpl.m.I=a.mpl.m.I+b.mpl.m.I, ans.type=MN_COMPLEX; } else if (a.type==MN_VECTOR) { if (b.type==MN_VECTOR) ans.mpl.m.I=a.mpl.m.I+b.mpl.m.I, ans.mpl.m.J=a.mpl.m.J+b.mpl.m.J, ans.mpl.m.K=a.mpl.m.K+b.mpl.m.K, ans.type=MN_VECTOR; } return(ans); } math_node mpl_sub(math_node a,math_node b) { math_node ans; ans.type=MN_NULL; if (a.type==MN_REAL) { if (b.type==MN_REAL) ans.mpl.m.R=a.mpl.m.R-b.mpl.m.R, ans.type=MN_REAL; else if (b.type==MN_COMPLEX) ans.mpl.m.R=a.mpl.m.R-b.mpl.m.R, ans.mpl.m.I=0-b.mpl.m.I, ans.type=MN_COMPLEX; } else if (a.type==MN_COMPLEX) { if (b.type==MN_REAL) ans.mpl.m.R=a.mpl.m.R-b.mpl.m.R, ans.mpl.m.I=a.mpl.m.I, ans.type=MN_COMPLEX; else if (b.type==MN_COMPLEX) ans.mpl.m.R=a.mpl.m.R-b.mpl.m.R, ans.mpl.m.I=a.mpl.m.I-b.mpl.m.I, ans.type=MN_COMPLEX; } else if (a.type==MN_VECTOR) { if (b.type==MN_VECTOR) ans.mpl.m.I=a.mpl.m.I-b.mpl.m.I, ans.mpl.m.J=a.mpl.m.J-b.mpl.m.J, ans.mpl.m.K=a.mpl.m.K-b.mpl.m.K, ans.type=MN_VECTOR; } return(ans); } math_node mpl_mul(math_node a,math_node b) { math_node ans; ans.type=MN_NULL; if (a.type==MN_REAL) { if (b.type==MN_REAL) ans.mpl.m.R=a.mpl.m.R*b.mpl.m.R, ans.type=MN_REAL; else if (b.type==MN_COMPLEX) ans.mpl.m.R=a.mpl.m.R*b.mpl.m.R, ans.mpl.m.I=a.mpl.m.R*b.mpl.m.I, ans.type=MN_COMPLEX; else if (b.type==MN_VECTOR) ans.mpl.m.I=a.mpl.m.R*b.mpl.m.I, ans.mpl.m.J=a.mpl.m.R*b.mpl.m.J, ans.mpl.m.K=a.mpl.m.R*b.mpl.m.K, ans.type=MN_VECTOR; } else if (a.type==MN_COMPLEX) { if (b.type==MN_REAL) ans.mpl.m.R=a.mpl.m.R*b.mpl.m.R, ans.mpl.m.I=a.mpl.m.I*b.mpl.m.R, ans.type=MN_COMPLEX; else if (b.type==MN_COMPLEX) ans.mpl.m.R=a.mpl.m.R*b.mpl.m.R-a.mpl.m.I*b.mpl.m.I, ans.mpl.m.I=a.mpl.m.R*b.mpl.m.I+a.mpl.m.I*b.mpl.m.R, ans.type=MN_COMPLEX; } else if (a.type==MN_VECTOR) { // Dot product if (b.type==MN_VECTOR) ans.mpl.m.R=a.mpl.m.I*b.mpl.m.I+a.mpl.m.J*b.mpl.m.J+a.mpl.m.K*b.mpl.m.K, ans.type=MN_REAL; else if (b.type==MN_REAL) ans.mpl.m.I=a.mpl.m.I*b.mpl.m.R, ans.mpl.m.J=a.mpl.m.J*b.mpl.m.R, ans.mpl.m.K=a.mpl.m.K*b.mpl.m.R, ans.type=MN_VECTOR; } return(ans); } math_node mpl_div(math_node a,math_node b) { math_node ans,opz; ans.type=MN_NULL; if (a.type==MN_REAL) { if (b.type==MN_REAL) ans.mpl.m.R=a.mpl.m.R/b.mpl.m.R, ans.type=MN_REAL; // ok else if (b.type==MN_COMPLEX) { opz=mpl_opz(b.Complex()); ans.mpl.m.R=a.mpl.m.R*opz.mpl.m.R, ans.mpl.m.I=a.mpl.m.R*opz.mpl.m.I, ans.type=MN_COMPLEX; } else if (b.type==MN_VECTOR) ans.mpl.m.I=a.mpl.m.R/b.mpl.m.I, ans.mpl.m.J=a.mpl.m.R/b.mpl.m.J, ans.mpl.m.K=a.mpl.m.R/b.mpl.m.K, ans.type=MN_VECTOR; } else if (a.type==MN_COMPLEX) { if (b.type==MN_REAL) ans.mpl.m.R=a.mpl.m.R*(1/b.mpl.m.R), ans.mpl.m.I=a.mpl.m.I*(1/b.mpl.m.R), ans.type=MN_COMPLEX; else if (b.type==MN_COMPLEX) { opz=mpl_opz(b.Complex()); ans.mpl.m.R=a.mpl.m.R*opz.mpl.m.R-a.mpl.m.I*opz.mpl.m.I, ans.mpl.m.I=a.mpl.m.R*opz.mpl.m.I+a.mpl.m.I*opz.mpl.m.R, ans.type=MN_COMPLEX; } } else if (a.type==MN_VECTOR) { if (b.type==MN_VECTOR) ans.mpl.m.I=a.mpl.m.I/b.mpl.m.I, ans.mpl.m.J=a.mpl.m.J/b.mpl.m.J, ans.mpl.m.K=a.mpl.m.K/b.mpl.m.K, ans.type=MN_VECTOR; else if (b.type==MN_REAL) ans.mpl.m.I=a.mpl.m.I/b.mpl.m.R, ans.mpl.m.J=a.mpl.m.J/b.mpl.m.R, ans.mpl.m.K=a.mpl.m.K/b.mpl.m.R, ans.type=MN_VECTOR; } return(ans); } math_node::math_node() { Clear(); } math_node::~math_node() { } math_node::math_node(mpl_real r) { mpl.real=r; type=MN_REAL; } math_node::math_node(mpl_complex z) { mpl.complex=z; type=MN_COMPLEX; } math_node::math_node(mpl_rational r) { mpl.rational=r; type=MN_RATIONAL; } math_node::math_node(mpl_vector v) { mpl.vector=v; type=MN_VECTOR; } void math_node::Clear() { mpl.m.R=mpl.m.I=mpl.m.K=mpl.m.J=0.0; type=MN_REAL; } bool math_node::isNull() { if (type==MN_NULL) return(true); return(false); } void math_node::SetReal(mpl_real x) { mpl.real=x; type=MN_REAL; } void math_node::SetComplex(mpl_complex z) { mpl.complex=z; type=MN_COMPLEX; } void math_node::SetRational(mpl_rational r) { mpl.rational=r; type=MN_RATIONAL; } mpl_real math_node::Real() { if (type==MN_RATIONAL) return(mpl.m.R/mpl.m.I); return(mpl.real); } mpl_complex math_node::Complex() { if (type==MN_REAL) mpl.complex.i=0; return(mpl.complex); } mpl_rational math_node::Rational() { return(mpl.rational); } mpl_vector math_node::Vector() { return(mpl.vector); }