new library dependency
This commit is contained in:
parent
d8e7398221
commit
bbd5b00af0
BIN
src/misc/lib/jscl.jar
Normal file
BIN
src/misc/lib/jscl.jar
Normal file
Binary file not shown.
58
src/misc/src/jscl/editorengine/EditorEngine.java
Normal file
58
src/misc/src/jscl/editorengine/EditorEngine.java
Normal file
@ -0,0 +1,58 @@
|
||||
package jscl.editorengine;
|
||||
|
||||
import jscl.editor.Engine;
|
||||
import jscl.editor.EngineException;
|
||||
import bsh.Interpreter;
|
||||
import bsh.EvalError;
|
||||
|
||||
public class EditorEngine extends Engine {
|
||||
Interpreter interp=new Interpreter();
|
||||
|
||||
public EditorEngine() throws EngineException {
|
||||
try {
|
||||
interp.eval("importCommands(\"/jscl/editorengine/commands\");\n"
|
||||
+"mml(x) { return tomathml(x); }\n");
|
||||
} catch (EvalError e) {
|
||||
throw new EngineException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public String eval(String str) throws EngineException {
|
||||
int n=str.length()-1;
|
||||
if(n<0 || "\n".equals(str.substring(n))) {
|
||||
exec(str);
|
||||
return str;
|
||||
} else return eval0(str);
|
||||
}
|
||||
|
||||
public void exec(String str) throws EngineException {
|
||||
try {
|
||||
interp.eval(str);
|
||||
} catch (EvalError e) {
|
||||
throw new EngineException(e);
|
||||
}
|
||||
}
|
||||
|
||||
String eval0(String str) throws EngineException {
|
||||
try {
|
||||
return interp.eval(commands(str)).toString();
|
||||
} catch (EvalError e) {
|
||||
throw new EngineException(e);
|
||||
}
|
||||
}
|
||||
|
||||
String commands(String str) {
|
||||
return commands(str,false);
|
||||
}
|
||||
|
||||
String commands(String str, boolean found) {
|
||||
for(int i=0;i<cmds.length;i++) {
|
||||
int n=str.length()-cmds[i].length()-1;
|
||||
if(n>=0 && (" "+cmds[i].toLowerCase()).equals(str.substring(n))) return commands(str.substring(0,n),true)+"."+cmds[i]+"()";
|
||||
}
|
||||
str=str.replaceAll("\n","");
|
||||
return found?"jscl.math.Expression.valueOf(\""+str+"\")":str;
|
||||
}
|
||||
|
||||
static final String cmds[]=new String[] {"expand","factorize","elementary","simplify","numeric","toMathML","toJava"};
|
||||
}
|
11
src/misc/src/jscl/editorengine/EditorEngineFactory.java
Normal file
11
src/misc/src/jscl/editorengine/EditorEngineFactory.java
Normal file
@ -0,0 +1,11 @@
|
||||
package jscl.editorengine;
|
||||
|
||||
import jscl.editor.Engine;
|
||||
import jscl.editor.EngineFactory;
|
||||
import jscl.editor.EngineException;
|
||||
|
||||
public class EditorEngineFactory extends EngineFactory {
|
||||
public Engine getEngine() throws EngineException {
|
||||
return new EditorEngine();
|
||||
}
|
||||
}
|
5
src/misc/src/jscl/editorengine/commands/elementary.bsh
Normal file
5
src/misc/src/jscl/editorengine/commands/elementary.bsh
Normal file
@ -0,0 +1,5 @@
|
||||
import jscl.math.Expression;
|
||||
|
||||
String elementary(String expr) {
|
||||
return Expression.valueOf(expr).elementary().toString();
|
||||
}
|
5
src/misc/src/jscl/editorengine/commands/expand.bsh
Normal file
5
src/misc/src/jscl/editorengine/commands/expand.bsh
Normal file
@ -0,0 +1,5 @@
|
||||
import jscl.math.Expression;
|
||||
|
||||
String expand(String expr) {
|
||||
return Expression.valueOf(expr).expand().toString();
|
||||
}
|
5
src/misc/src/jscl/editorengine/commands/factorize.bsh
Normal file
5
src/misc/src/jscl/editorengine/commands/factorize.bsh
Normal file
@ -0,0 +1,5 @@
|
||||
import jscl.math.Expression;
|
||||
|
||||
String factorize(String expr) {
|
||||
return Expression.valueOf(expr).factorize().toString();
|
||||
}
|
5
src/misc/src/jscl/editorengine/commands/numeric.bsh
Normal file
5
src/misc/src/jscl/editorengine/commands/numeric.bsh
Normal file
@ -0,0 +1,5 @@
|
||||
import jscl.math.Expression;
|
||||
|
||||
String numeric(String expr) {
|
||||
return Expression.valueOf(expr).numeric().toString();
|
||||
}
|
5
src/misc/src/jscl/editorengine/commands/simplify.bsh
Normal file
5
src/misc/src/jscl/editorengine/commands/simplify.bsh
Normal file
@ -0,0 +1,5 @@
|
||||
import jscl.math.Expression;
|
||||
|
||||
String simplify(String expr) {
|
||||
return Expression.valueOf(expr).simplify().toString();
|
||||
}
|
5
src/misc/src/jscl/editorengine/commands/tojava.bsh
Normal file
5
src/misc/src/jscl/editorengine/commands/tojava.bsh
Normal file
@ -0,0 +1,5 @@
|
||||
import jscl.math.Expression;
|
||||
|
||||
String tojava(String expr) {
|
||||
return Expression.valueOf(expr).toJava();
|
||||
}
|
5
src/misc/src/jscl/editorengine/commands/tomathml.bsh
Normal file
5
src/misc/src/jscl/editorengine/commands/tomathml.bsh
Normal file
@ -0,0 +1,5 @@
|
||||
import jscl.math.Expression;
|
||||
|
||||
String tomathml(String expr) {
|
||||
return Expression.valueOf(expr).toMathML();
|
||||
}
|
228
src/misc/src/jscl/math/Antiderivative.java
Normal file
228
src/misc/src/jscl/math/Antiderivative.java
Normal file
@ -0,0 +1,228 @@
|
||||
package jscl.math;
|
||||
|
||||
import jscl.math.function.Frac;
|
||||
import jscl.math.function.Inv;
|
||||
import jscl.math.function.Log;
|
||||
import jscl.math.function.Pow;
|
||||
import jscl.math.function.Root;
|
||||
import jscl.math.polynomial.Monomial;
|
||||
import jscl.math.polynomial.Polynomial;
|
||||
import jscl.math.polynomial.UnivariatePolynomial;
|
||||
|
||||
public class Antiderivative {
|
||||
UnivariatePolynomial factory;
|
||||
PolynomialWithSyzygy syzygy;
|
||||
Generic result;
|
||||
|
||||
Antiderivative(Variable variable) {
|
||||
factory=(UnivariatePolynomial)Polynomial.factory(variable);
|
||||
syzygy=(PolynomialWithSyzygy)PolynomialWithSyzygy.factory(variable);
|
||||
}
|
||||
|
||||
public static Generic compute(Frac frac, Variable variable) {
|
||||
Antiderivative s=new Antiderivative(variable);
|
||||
s.compute(frac);
|
||||
return s.getValue();
|
||||
}
|
||||
|
||||
public static Generic compute(Root root, Variable variable) throws NotIntegrableException {
|
||||
int d=root.degree();
|
||||
Generic a[]=root.parameters();
|
||||
boolean b=d>0;
|
||||
b=b && a[0].negate().isIdentity(variable);
|
||||
for(int i=1;i<d;i++) b=b && a[i].signum()==0;
|
||||
b=b && a[d].compareTo(JSCLInteger.valueOf(1))==0;
|
||||
if(b) {
|
||||
return new Pow(
|
||||
a[0].negate(),
|
||||
new Inv(JSCLInteger.valueOf(d)).evaluate()
|
||||
).antiderivative(0);
|
||||
} else throw new NotIntegrableException();
|
||||
}
|
||||
|
||||
void compute(Frac frac) {
|
||||
Debug.println("antiderivative");
|
||||
Debug.increment();
|
||||
Generic g[]=frac.parameters();
|
||||
Generic r[]=reduce(g[0],g[1]);
|
||||
r=divideAndRemainder(r[0],r[1]);
|
||||
Generic s=new Inv(r[2]).evaluate();
|
||||
Generic p=r[0].multiply(s);
|
||||
Generic a=r[1].multiply(s);
|
||||
result=p.antiderivative(factory.variable()).add(hermite(a,g[1]));
|
||||
Debug.decrement();
|
||||
}
|
||||
|
||||
Generic[] reduce(Generic n, Generic d) {
|
||||
Debug.println("reduce("+n+", "+d+")");
|
||||
Polynomial pn=factory.valueof(n);
|
||||
Polynomial pd=factory.valueof(d);
|
||||
Polynomial gcd=pn.gcd(pd);
|
||||
return new Generic[] {
|
||||
pn.divide(gcd).genericValue(),
|
||||
pd.divide(gcd).genericValue()
|
||||
};
|
||||
}
|
||||
|
||||
Generic[] divideAndRemainder(Generic n, Generic d) {
|
||||
Debug.println("divideAndRemainder("+n+", "+d+")");
|
||||
Polynomial pn=syzygy.valueof(n,0);
|
||||
Polynomial pd=syzygy.valueof(d,1);
|
||||
PolynomialWithSyzygy pr=(PolynomialWithSyzygy)pn.remainderUpToCoefficient(pd);
|
||||
return new Generic[] {
|
||||
pr.syzygy[1].genericValue().negate(),
|
||||
pr.genericValue(),
|
||||
pr.syzygy[0].genericValue()
|
||||
};
|
||||
}
|
||||
|
||||
Generic[] bezout(Generic a, Generic b) {
|
||||
Debug.println("bezout("+a+", "+b+")");
|
||||
Polynomial pa=syzygy.valueof(a,0);
|
||||
Polynomial pb=syzygy.valueof(b,1);
|
||||
PolynomialWithSyzygy gcd=(PolynomialWithSyzygy)pa.gcd(pb);
|
||||
return new Generic[] {
|
||||
gcd.syzygy[0].genericValue(),
|
||||
gcd.syzygy[1].genericValue(),
|
||||
gcd.genericValue()
|
||||
};
|
||||
}
|
||||
|
||||
Generic hermite(Generic a, Generic d) {
|
||||
Debug.println("hermite("+a+", "+d+")");
|
||||
UnivariatePolynomial sd[]=((UnivariatePolynomial)factory.valueof(d)).squarefreeDecomposition();
|
||||
int m=sd.length-1;
|
||||
if(m<2) return trager(a,d);
|
||||
else {
|
||||
Generic u=sd[0].genericValue();
|
||||
for(int i=1;i<m;i++) {
|
||||
u=u.multiply(sd[i].genericValue().pow(i));
|
||||
}
|
||||
Generic v=sd[m].genericValue();
|
||||
Generic vprime=sd[m].derivative().genericValue();
|
||||
Generic uvprime=u.multiply(vprime);
|
||||
Generic r[]=bezout(uvprime,v);
|
||||
Generic b=r[0].multiply(a);
|
||||
Generic c=r[1].multiply(a);
|
||||
Generic s=r[2];
|
||||
r=divideAndRemainder(b,v);
|
||||
b=r[1];
|
||||
c=c.multiply(r[2]).add(r[0].multiply(uvprime));
|
||||
s=new Inv(s.multiply(r[2]).multiply(JSCLInteger.valueOf(1-m))).evaluate();
|
||||
b=b.multiply(s);
|
||||
c=c.multiply(s);
|
||||
Generic bprime=((UnivariatePolynomial)factory.valueof(b)).derivative().genericValue();
|
||||
return new Frac(b,v.pow(m-1)).evaluate().add(hermite(JSCLInteger.valueOf(1-m).multiply(c).subtract(u.multiply(bprime)),u.multiply(v.pow(m-1))));
|
||||
}
|
||||
}
|
||||
|
||||
Generic trager(Generic a, Generic d) {
|
||||
Debug.println("trager("+a+", "+d+")");
|
||||
Variable t=new TechnicalVariable("t");
|
||||
UnivariatePolynomial pd=(UnivariatePolynomial)factory.valueof(d);
|
||||
UnivariatePolynomial pa=(UnivariatePolynomial)factory.valueof(a).subtract(pd.derivative().multiply(t.expressionValue()));
|
||||
UnivariatePolynomial rs[]=pd.remainderSequence(pa);
|
||||
Polynomial fact=UnivariatePolynomial.factory(t);
|
||||
for(int i=0;i<rs.length;i++) if(rs[i]!=null) rs[i]=(UnivariatePolynomial)fact.valueof((i>0?rs[i].normalize():rs[i]).genericValue());
|
||||
UnivariatePolynomial q[]=rs[0].squarefreeDecomposition();
|
||||
int m=q.length-1;
|
||||
Generic s=JSCLInteger.valueOf(0);
|
||||
for(int i=1;i<=m;i++) {
|
||||
for(int j=0;j<q[i].degree();j++) {
|
||||
Generic a2=new Root(q[i],j).evaluate();
|
||||
s=s.add(a2.multiply(new Log(i==pd.degree()?d:rs[i].substitute(a2)).evaluate()));
|
||||
}
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
Generic getValue() {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
class PolynomialWithSyzygy extends UnivariatePolynomial {
|
||||
Polynomial syzygy[]=new Polynomial[2];
|
||||
|
||||
PolynomialWithSyzygy(Variable variable) {
|
||||
super(variable);
|
||||
}
|
||||
|
||||
public Polynomial subtract(Polynomial polynomial) {
|
||||
PolynomialWithSyzygy p2=(PolynomialWithSyzygy)polynomial;
|
||||
PolynomialWithSyzygy p=(PolynomialWithSyzygy)super.subtract(p2);
|
||||
for(int i=0;i<syzygy.length;i++) p.syzygy[i]=syzygy[i].subtract(p2.syzygy[i]);
|
||||
return p;
|
||||
}
|
||||
|
||||
public Polynomial multiply(Generic generic) {
|
||||
PolynomialWithSyzygy p=(PolynomialWithSyzygy)super.multiply(generic);
|
||||
for(int i=0;i<syzygy.length;i++) p.syzygy[i]=syzygy[i].multiply(generic);
|
||||
return p;
|
||||
}
|
||||
|
||||
public Polynomial multiply(Monomial monomial, Generic generic) {
|
||||
PolynomialWithSyzygy p=(PolynomialWithSyzygy)super.multiply(monomial,generic);
|
||||
for(int i=0;i<syzygy.length;i++) p.syzygy[i]=syzygy[i].multiply(monomial).multiply(generic);
|
||||
return p;
|
||||
}
|
||||
|
||||
public Polynomial divide(Generic generic) throws ArithmeticException {
|
||||
PolynomialWithSyzygy p=(PolynomialWithSyzygy)super.divide(generic);
|
||||
for(int i=0;i<syzygy.length;i++) p.syzygy[i]=syzygy[i].divide(generic);
|
||||
return p;
|
||||
}
|
||||
|
||||
public Polynomial remainderUpToCoefficient(Polynomial polynomial) throws ArithmeticException {
|
||||
PolynomialWithSyzygy p=this;
|
||||
PolynomialWithSyzygy q=(PolynomialWithSyzygy)polynomial;
|
||||
if(p.signum()==0) return p;
|
||||
int d=p.degree();
|
||||
int d2=q.degree();
|
||||
for(int i=d-d2;i>=0;i--) {
|
||||
Generic c1=p.get(i+d2);
|
||||
Generic c2=q.get(d2);
|
||||
Generic c=c1.gcd(c2);
|
||||
c1=c1.divide(c);
|
||||
c2=c2.divide(c);
|
||||
p=(PolynomialWithSyzygy)p.multiply(c2).subtract(q.multiply(monomial(Literal.valueOf(variable,i)),c1)).normalize();
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
public Polynomial gcd(Polynomial polynomial) {
|
||||
Polynomial p=this;
|
||||
Polynomial q=polynomial;
|
||||
while(q.signum()!=0) {
|
||||
Polynomial r=p.remainderUpToCoefficient(q);
|
||||
p=q;
|
||||
q=r;
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
public Generic gcd() {
|
||||
Generic a=super.gcd();
|
||||
for(int i=0;i<syzygy.length;i++) a=a.gcd(syzygy[i].gcd());
|
||||
return a.signum()==signum()?a:a.negate();
|
||||
}
|
||||
|
||||
public PolynomialWithSyzygy valueof(Generic generic, int n) {
|
||||
PolynomialWithSyzygy p=(PolynomialWithSyzygy)newinstance();
|
||||
p.init(generic,n);
|
||||
return p;
|
||||
}
|
||||
|
||||
public static Polynomial factory(Variable variable) {
|
||||
return new PolynomialWithSyzygy(variable);
|
||||
}
|
||||
|
||||
void init(Generic generic, int n) {
|
||||
init(generic);
|
||||
for(int i=0;i<syzygy.length;i++) syzygy[i]=Polynomial.factory(variable).valueof(JSCLInteger.valueOf(i==n?1:0));
|
||||
}
|
||||
|
||||
protected UnivariatePolynomial newinstance() {
|
||||
return new PolynomialWithSyzygy(variable);
|
||||
}
|
||||
}
|
8
src/misc/src/jscl/math/Arithmetic.java
Normal file
8
src/misc/src/jscl/math/Arithmetic.java
Normal file
@ -0,0 +1,8 @@
|
||||
package jscl.math;
|
||||
|
||||
public interface Arithmetic {
|
||||
abstract Arithmetic add(Arithmetic arithmetic);
|
||||
abstract Arithmetic subtract(Arithmetic arithmetic);
|
||||
abstract Arithmetic multiply(Arithmetic arithmetic);
|
||||
abstract Arithmetic divide(Arithmetic arithmetic) throws ArithmeticException;
|
||||
}
|
38
src/misc/src/jscl/math/Debug.java
Normal file
38
src/misc/src/jscl/math/Debug.java
Normal file
@ -0,0 +1,38 @@
|
||||
package jscl.math;
|
||||
|
||||
import java.io.PrintStream;
|
||||
|
||||
public class Debug {
|
||||
static PrintStream out;
|
||||
static int indentation;
|
||||
|
||||
private Debug() {}
|
||||
|
||||
public static void println(Object x) {
|
||||
if(out==null);
|
||||
else {
|
||||
for(int i=0;i<indentation;i++) out.print(' ');
|
||||
out.println(x);
|
||||
}
|
||||
}
|
||||
|
||||
public static void setOutputStream(PrintStream out) {
|
||||
Debug.out=out;
|
||||
}
|
||||
|
||||
public static PrintStream getOutputStream() {
|
||||
return out;
|
||||
}
|
||||
|
||||
public static void increment() {
|
||||
indentation++;
|
||||
}
|
||||
|
||||
public static void decrement() {
|
||||
indentation--;
|
||||
}
|
||||
|
||||
public static void reset() {
|
||||
indentation=0;
|
||||
}
|
||||
}
|
44
src/misc/src/jscl/math/DoubleVariable.java
Normal file
44
src/misc/src/jscl/math/DoubleVariable.java
Normal file
@ -0,0 +1,44 @@
|
||||
package jscl.math;
|
||||
|
||||
public class DoubleVariable extends GenericVariable {
|
||||
public DoubleVariable(Generic generic) {
|
||||
super(generic);
|
||||
}
|
||||
|
||||
public JSCLInteger symbolic() {
|
||||
return ((NumericWrapper)content).integerValue();
|
||||
}
|
||||
|
||||
public Generic antiderivative(Variable variable) throws NotIntegrableException {
|
||||
return expressionValue().multiply(variable.expressionValue());
|
||||
}
|
||||
|
||||
public Generic derivative(Variable variable) {
|
||||
return JSCLInteger.valueOf(0);
|
||||
}
|
||||
|
||||
public Generic substitute(Variable variable, Generic generic) {
|
||||
if(isIdentity(variable)) return generic;
|
||||
else return expressionValue();
|
||||
}
|
||||
|
||||
public Generic expand() {
|
||||
return expressionValue();
|
||||
}
|
||||
|
||||
public Generic factorize() {
|
||||
return expressionValue();
|
||||
}
|
||||
|
||||
public Generic elementary() {
|
||||
return expressionValue();
|
||||
}
|
||||
|
||||
public Generic simplify() {
|
||||
return expressionValue();
|
||||
}
|
||||
|
||||
protected Variable newinstance() {
|
||||
return new DoubleVariable(null);
|
||||
}
|
||||
}
|
709
src/misc/src/jscl/math/Expression.java
Normal file
709
src/misc/src/jscl/math/Expression.java
Normal file
@ -0,0 +1,709 @@
|
||||
package jscl.math;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import jscl.math.function.Frac;
|
||||
import jscl.math.function.Inv;
|
||||
import jscl.math.polynomial.Polynomial;
|
||||
import jscl.math.polynomial.UnivariatePolynomial;
|
||||
import jscl.mathml.MathML;
|
||||
import jscl.text.ExpressionParser;
|
||||
import jscl.text.ParseException;
|
||||
import jscl.text.Parser;
|
||||
import jscl.util.ArrayUtils;
|
||||
|
||||
public class Expression extends Generic {
|
||||
Literal literal[];
|
||||
JSCLInteger coef[];
|
||||
int size;
|
||||
|
||||
Expression() {}
|
||||
|
||||
Expression(int size) {
|
||||
init(size);
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public Literal literal(int n) {
|
||||
return literal[n];
|
||||
}
|
||||
|
||||
public JSCLInteger coef(int n) {
|
||||
return coef[n];
|
||||
}
|
||||
|
||||
void init(int size) {
|
||||
literal=new Literal[size];
|
||||
coef=new JSCLInteger[size];
|
||||
this.size=size;
|
||||
}
|
||||
|
||||
void resize(int size) {
|
||||
int length=literal.length;
|
||||
if(size<length) {
|
||||
Literal literal[]=new Literal[size];
|
||||
JSCLInteger coef[]=new JSCLInteger[size];
|
||||
System.arraycopy(this.literal,length-size,literal,0,size);
|
||||
System.arraycopy(this.coef,length-size,coef,0,size);
|
||||
this.literal=literal;
|
||||
this.coef=coef;
|
||||
this.size=size;
|
||||
}
|
||||
}
|
||||
|
||||
public Expression add(Expression expression) {
|
||||
Expression ex=newinstance(size+expression.size);
|
||||
int i=ex.size;
|
||||
int i1=size;
|
||||
int i2=expression.size;
|
||||
Literal l1=i1>0?literal[--i1]:null;
|
||||
Literal l2=i2>0?expression.literal[--i2]:null;
|
||||
while(l1!=null || l2!=null) {
|
||||
int c=l1==null?1:(l2==null?-1:-l1.compareTo(l2));
|
||||
if(c<0) {
|
||||
JSCLInteger en=coef[i1];
|
||||
--i;
|
||||
ex.literal[i]=l1;
|
||||
ex.coef[i]=en;
|
||||
l1=i1>0?literal[--i1]:null;
|
||||
} else if(c>0) {
|
||||
JSCLInteger en=expression.coef[i2];
|
||||
--i;
|
||||
ex.literal[i]=l2;
|
||||
ex.coef[i]=en;
|
||||
l2=i2>0?expression.literal[--i2]:null;
|
||||
} else {
|
||||
JSCLInteger en=coef[i1].add(expression.coef[i2]);
|
||||
if(en.signum()!=0) {
|
||||
--i;
|
||||
ex.literal[i]=l1;
|
||||
ex.coef[i]=en;
|
||||
}
|
||||
l1=i1>0?literal[--i1]:null;
|
||||
l2=i2>0?expression.literal[--i2]:null;
|
||||
}
|
||||
}
|
||||
ex.resize(ex.size-i);
|
||||
return ex;
|
||||
}
|
||||
|
||||
public Generic add(Generic generic) {
|
||||
if(generic instanceof Expression) {
|
||||
return add((Expression)generic);
|
||||
} else if(generic instanceof JSCLInteger || generic instanceof Rational) {
|
||||
return add(valueof(generic));
|
||||
} else {
|
||||
return generic.valueof(this).add(generic);
|
||||
}
|
||||
}
|
||||
|
||||
public Expression subtract(Expression expression) {
|
||||
return multiplyAndAdd(Literal.valueOf(),JSCLInteger.valueOf(-1),expression);
|
||||
}
|
||||
|
||||
public Generic subtract(Generic generic) {
|
||||
if(generic instanceof Expression) {
|
||||
return subtract((Expression)generic);
|
||||
} else if(generic instanceof JSCLInteger || generic instanceof Rational) {
|
||||
return subtract(valueof(generic));
|
||||
} else {
|
||||
return generic.valueof(this).subtract(generic);
|
||||
}
|
||||
}
|
||||
|
||||
Expression multiplyAndAdd(Literal lit, JSCLInteger integer, Expression expression) {
|
||||
if(integer.signum()==0) return this;
|
||||
Expression ex=newinstance(size+expression.size);
|
||||
int i=ex.size;
|
||||
int i1=size;
|
||||
int i2=expression.size;
|
||||
Literal l1=i1>0?literal[--i1]:null;
|
||||
Literal l2=i2>0?expression.literal[--i2].multiply(lit):null;
|
||||
while(l1!=null || l2!=null) {
|
||||
int c=l1==null?1:(l2==null?-1:-l1.compareTo(l2));
|
||||
if(c<0) {
|
||||
JSCLInteger en=coef[i1];
|
||||
--i;
|
||||
ex.literal[i]=l1;
|
||||
ex.coef[i]=en;
|
||||
l1=i1>0?literal[--i1]:null;
|
||||
} else if(c>0) {
|
||||
JSCLInteger en=expression.coef[i2].multiply(integer);
|
||||
--i;
|
||||
ex.literal[i]=l2;
|
||||
ex.coef[i]=en;
|
||||
l2=i2>0?expression.literal[--i2].multiply(lit):null;
|
||||
} else {
|
||||
JSCLInteger en=coef[i1].add(expression.coef[i2].multiply(integer));
|
||||
if(en.signum()!=0) {
|
||||
--i;
|
||||
ex.literal[i]=l1;
|
||||
ex.coef[i]=en;
|
||||
}
|
||||
l1=i1>0?literal[--i1]:null;
|
||||
l2=i2>0?expression.literal[--i2].multiply(lit):null;
|
||||
}
|
||||
}
|
||||
ex.resize(ex.size-i);
|
||||
return ex;
|
||||
}
|
||||
|
||||
public Expression multiply(Expression expression) {
|
||||
Expression ex=newinstance(0);
|
||||
for(int i=0;i<size;i++) ex=ex.multiplyAndAdd(literal[i],coef[i],expression);
|
||||
return ex;
|
||||
}
|
||||
|
||||
public Generic multiply(Generic generic) {
|
||||
if(generic instanceof Expression) {
|
||||
return multiply((Expression)generic);
|
||||
} else if(generic instanceof JSCLInteger) {
|
||||
return multiply(valueof(generic));
|
||||
} else if(generic instanceof Rational) {
|
||||
return multiply(valueof(generic));
|
||||
} else {
|
||||
return generic.multiply(this);
|
||||
}
|
||||
}
|
||||
|
||||
public Generic divide(Generic generic) throws ArithmeticException {
|
||||
Generic a[]=divideAndRemainder(generic);
|
||||
if(a[1].signum()==0) return a[0];
|
||||
else throw new NotDivisibleException();
|
||||
}
|
||||
|
||||
public Generic[] divideAndRemainder(Generic generic) throws ArithmeticException {
|
||||
if(generic instanceof Expression) {
|
||||
Expression ex=(Expression)generic;
|
||||
Literal l1=literalScm();
|
||||
Literal l2=ex.literalScm();
|
||||
Literal l=(Literal)l1.gcd(l2);
|
||||
Variable va[]=l.variables();
|
||||
if(va.length==0) {
|
||||
if(signum()==0 && ex.signum()!=0) return new Generic[] {this,JSCLInteger.valueOf(0)};
|
||||
else try {
|
||||
return divideAndRemainder(ex.integerValue());
|
||||
} catch (NotIntegerException e) {
|
||||
return new Generic[] {JSCLInteger.valueOf(0),this};
|
||||
}
|
||||
} else {
|
||||
Polynomial fact=Polynomial.factory(va[0]);
|
||||
Polynomial p[]=fact.valueof(this).divideAndRemainder(fact.valueof(ex));
|
||||
return new Generic[] {p[0].genericValue(),p[1].genericValue()};
|
||||
}
|
||||
} else if(generic instanceof JSCLInteger) {
|
||||
try {
|
||||
Expression ex=newinstance(size);
|
||||
for(int i=0;i<size;i++) {
|
||||
ex.literal[i]=literal[i];
|
||||
ex.coef[i]=coef[i].divide((JSCLInteger)generic);
|
||||
}
|
||||
return new Generic[] {ex,JSCLInteger.valueOf(0)};
|
||||
} catch (NotDivisibleException e) {
|
||||
return new Generic[] {JSCLInteger.valueOf(0),this};
|
||||
}
|
||||
} else if(generic instanceof Rational) {
|
||||
return divideAndRemainder(valueof(generic));
|
||||
} else {
|
||||
return generic.valueof(this).divideAndRemainder(generic);
|
||||
}
|
||||
}
|
||||
|
||||
public Generic gcd(Generic generic) {
|
||||
if(generic instanceof Expression) {
|
||||
Expression ex=(Expression)generic;
|
||||
Literal l1=literalScm();
|
||||
Literal l2=ex.literalScm();
|
||||
Literal l=(Literal)l1.gcd(l2);
|
||||
Variable va[]=l.variables();
|
||||
if(va.length==0) {
|
||||
if(signum()==0) return ex;
|
||||
else return gcd(ex.gcd());
|
||||
} else {
|
||||
Polynomial fact=Polynomial.factory(va[0]);
|
||||
return fact.valueof(this).gcd(fact.valueof(ex)).genericValue();
|
||||
}
|
||||
} else if(generic instanceof JSCLInteger) {
|
||||
if(generic.signum()==0) return this;
|
||||
else return gcd().gcd(generic);
|
||||
} else if(generic instanceof Rational) {
|
||||
return gcd(valueof(generic));
|
||||
} else {
|
||||
return generic.valueof(this).gcd(generic);
|
||||
}
|
||||
}
|
||||
|
||||
public Generic gcd() {
|
||||
JSCLInteger en=JSCLInteger.valueOf(0);
|
||||
for(int i=size-1;i>=0;i--) en=en.gcd(coef[i]);
|
||||
return en;
|
||||
}
|
||||
|
||||
public Literal literalScm() {
|
||||
Literal l=Literal.valueOf();
|
||||
for(int i=0;i<size;i++) l=l.scm(literal[i]);
|
||||
return l;
|
||||
}
|
||||
|
||||
public Generic negate() {
|
||||
return multiply(JSCLInteger.valueOf(-1));
|
||||
}
|
||||
|
||||
public int signum() {
|
||||
return size==0?0:coef[0].signum();
|
||||
}
|
||||
|
||||
public int degree() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public Generic antiderivative(Variable variable) throws NotIntegrableException {
|
||||
if(isPolynomial(variable)) {
|
||||
return ((UnivariatePolynomial)Polynomial.factory(variable).valueof(this)).antiderivative().genericValue();
|
||||
} else {
|
||||
try {
|
||||
Variable v=variableValue();
|
||||
try {
|
||||
return v.antiderivative(variable);
|
||||
} catch (NotIntegrableException e) {
|
||||
if(v instanceof Frac) {
|
||||
Generic g[]=((Frac)v).parameters();
|
||||
if(g[1].isConstant(variable)) {
|
||||
return new Inv(g[1]).evaluate().multiply(g[0].antiderivative(variable));
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (NotVariableException e) {
|
||||
Generic a[]=sumValue();
|
||||
if(a.length>1) {
|
||||
Generic s=JSCLInteger.valueOf(0);
|
||||
for(int i=0;i<a.length;i++) {
|
||||
s=s.add(a[i].antiderivative(variable));
|
||||
}
|
||||
return s;
|
||||
} else {
|
||||
Generic p[]=a[0].productValue();
|
||||
Generic s=JSCLInteger.valueOf(1);
|
||||
Generic t=JSCLInteger.valueOf(1);
|
||||
for(int i=0;i<p.length;i++) {
|
||||
if(p[i].isConstant(variable)) s=s.multiply(p[i]);
|
||||
else t=t.multiply(p[i]);
|
||||
}
|
||||
if(s.compareTo(JSCLInteger.valueOf(1))==0);
|
||||
else return s.multiply(t.antiderivative(variable));
|
||||
}
|
||||
}
|
||||
}
|
||||
throw new NotIntegrableException();
|
||||
}
|
||||
|
||||
public Generic derivative(Variable variable) {
|
||||
Generic s=JSCLInteger.valueOf(0);
|
||||
Literal l=literalScm();
|
||||
int n=l.size;
|
||||
for(int i=0;i<n;i++) {
|
||||
Variable v=l.variable[i];
|
||||
Generic a=((UnivariatePolynomial)Polynomial.factory(v).valueof(this)).derivative(variable).genericValue();
|
||||
s=s.add(a);
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
public Generic substitute(Variable variable, Generic generic) {
|
||||
Map m=literalScm().content();
|
||||
Iterator it=m.entrySet().iterator();
|
||||
while(it.hasNext()) {
|
||||
Map.Entry e=(Map.Entry)it.next();
|
||||
Variable v=(Variable)e.getKey();
|
||||
e.setValue(v.substitute(variable,generic));
|
||||
}
|
||||
return substitute(m);
|
||||
}
|
||||
|
||||
Generic substitute(Map map) {
|
||||
Generic s=JSCLInteger.valueOf(0);
|
||||
for(int i=0;i<size;i++) {
|
||||
Literal l=literal[i];
|
||||
Generic a=coef[i];
|
||||
int m=l.size;
|
||||
for(int j=0;j<m;j++) {
|
||||
Variable v=l.variable[j];
|
||||
int c=l.power[j];
|
||||
Generic b=(Generic)map.get(v);
|
||||
b=b.pow(c);
|
||||
if(Matrix.product(a,b)) throw new ArithmeticException();
|
||||
a=a.multiply(b);
|
||||
}
|
||||
s=s.add(a);
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
public Generic expand() {
|
||||
Map m=literalScm().content();
|
||||
Iterator it=m.entrySet().iterator();
|
||||
while(it.hasNext()) {
|
||||
Map.Entry e=(Map.Entry)it.next();
|
||||
Variable v=(Variable)e.getKey();
|
||||
e.setValue(v.expand());
|
||||
}
|
||||
return substitute(m);
|
||||
}
|
||||
|
||||
public Generic factorize() {
|
||||
Map m=literalScm().content();
|
||||
Iterator it=m.entrySet().iterator();
|
||||
while(it.hasNext()) {
|
||||
Map.Entry e=(Map.Entry)it.next();
|
||||
Variable v=(Variable)e.getKey();
|
||||
e.setValue(v.factorize());
|
||||
}
|
||||
Generic a=substitute(m);
|
||||
return Factorization.compute(a);
|
||||
}
|
||||
|
||||
public Generic elementary() {
|
||||
Map m=literalScm().content();
|
||||
Iterator it=m.entrySet().iterator();
|
||||
while(it.hasNext()) {
|
||||
Map.Entry e=(Map.Entry)it.next();
|
||||
Variable v=(Variable)e.getKey();
|
||||
e.setValue(v.elementary());
|
||||
}
|
||||
return substitute(m);
|
||||
}
|
||||
|
||||
public Generic simplify() {
|
||||
return Simplification.compute(this);
|
||||
}
|
||||
|
||||
public Generic numeric() {
|
||||
try {
|
||||
return integerValue().numeric();
|
||||
} catch (NotIntegerException ex) {
|
||||
Map m=literalScm().content();
|
||||
Iterator it=m.entrySet().iterator();
|
||||
while(it.hasNext()) {
|
||||
Map.Entry e=(Map.Entry)it.next();
|
||||
Variable v=(Variable)e.getKey();
|
||||
e.setValue(v.numeric());
|
||||
}
|
||||
return substitute(m);
|
||||
}
|
||||
}
|
||||
|
||||
public Generic valueof(Generic generic) {
|
||||
Expression ex=newinstance(0);
|
||||
ex.init(generic);
|
||||
return ex;
|
||||
}
|
||||
|
||||
public Generic[] sumValue() {
|
||||
Generic a[]=new Generic[size];
|
||||
for(int i=0;i<a.length;i++) a[i]=valueOf(literal[i],coef[i]);
|
||||
return a;
|
||||
}
|
||||
|
||||
public Generic[] productValue() throws NotProductException {
|
||||
if(size==0) return new Generic[] {JSCLInteger.valueOf(0)};
|
||||
else if(size==1) {
|
||||
Literal l=literal[0];
|
||||
JSCLInteger en=coef[0];
|
||||
Generic p[]=l.productValue();
|
||||
if(en.compareTo(JSCLInteger.valueOf(1))==0) return p;
|
||||
else {
|
||||
Generic a[]=new Generic[p.length+1];
|
||||
for(int i=0;i<p.length;i++) a[i+1]=p[i];
|
||||
a[0]=en;
|
||||
return a;
|
||||
}
|
||||
} else throw new NotProductException();
|
||||
}
|
||||
|
||||
public Power powerValue() throws NotPowerException {
|
||||
if(size==0) return new Power(JSCLInteger.valueOf(0),1);
|
||||
else if(size==1) {
|
||||
Literal l=literal[0];
|
||||
JSCLInteger en=coef[0];
|
||||
if(en.compareTo(JSCLInteger.valueOf(1))==0) return l.powerValue();
|
||||
else if(l.degree()==0) return en.powerValue();
|
||||
else throw new NotPowerException();
|
||||
} else throw new NotPowerException();
|
||||
}
|
||||
|
||||
public Expression expressionValue() throws NotExpressionException {
|
||||
return this;
|
||||
}
|
||||
|
||||
public JSCLInteger integerValue() throws NotIntegerException {
|
||||
if(size==0) return JSCLInteger.valueOf(0);
|
||||
else if(size==1) {
|
||||
Literal l=literal[0];
|
||||
JSCLInteger en=coef[0];
|
||||
if(l.degree()==0) return en;
|
||||
else throw new NotIntegerException();
|
||||
} else throw new NotIntegerException();
|
||||
}
|
||||
|
||||
public Variable variableValue() throws NotVariableException {
|
||||
if(size==0) throw new NotVariableException();
|
||||
else if(size==1) {
|
||||
Literal l=literal[0];
|
||||
JSCLInteger en=coef[0];
|
||||
if(en.compareTo(JSCLInteger.valueOf(1))==0) return l.variableValue();
|
||||
else throw new NotVariableException();
|
||||
} else throw new NotVariableException();
|
||||
}
|
||||
|
||||
public Variable[] variables() {
|
||||
return literalScm().variables();
|
||||
}
|
||||
|
||||
public static Variable[] variables(Generic generic[]) {
|
||||
List l=new ArrayList();
|
||||
for(int i=0;i<generic.length;i++) {
|
||||
Variable va[]=generic[i].variables();
|
||||
for(int j=0;j<va.length;j++) {
|
||||
Variable v=va[j];
|
||||
if(l.contains(v));
|
||||
else l.add(v);
|
||||
}
|
||||
}
|
||||
return (Variable[])ArrayUtils.toArray(l,new Variable[l.size()]);
|
||||
}
|
||||
|
||||
public boolean isPolynomial(Variable variable) {
|
||||
boolean s=true;
|
||||
Literal l=literalScm();
|
||||
int n=l.size;
|
||||
for(int i=0;i<n;i++) {
|
||||
Variable v=l.variable[i];
|
||||
s=s && (v.isConstant(variable) || v.isIdentity(variable));
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
public boolean isConstant(Variable variable) {
|
||||
boolean s=true;
|
||||
Literal l=literalScm();
|
||||
int n=l.size;
|
||||
for(int i=0;i<n;i++) {
|
||||
Variable v=l.variable[i];
|
||||
s=s && v.isConstant(variable);
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
public JSCLVector grad(Variable variable[]) {
|
||||
Generic v[]=new Generic[variable.length];
|
||||
for(int i=0;i<variable.length;i++) v[i]=derivative(variable[i]);
|
||||
return new JSCLVector(v);
|
||||
}
|
||||
|
||||
public Generic laplacian(Variable variable[]) {
|
||||
return grad(variable).divergence(variable);
|
||||
}
|
||||
|
||||
public Generic dalembertian(Variable variable[]) {
|
||||
Generic a=derivative(variable[0]).derivative(variable[0]);
|
||||
for(int i=1;i<4;i++) a=a.subtract(derivative(variable[i]).derivative(variable[i]));
|
||||
return a;
|
||||
}
|
||||
|
||||
public int compareTo(Expression expression) {
|
||||
int i1=size;
|
||||
int i2=expression.size;
|
||||
Literal l1=i1==0?null:literal[--i1];
|
||||
Literal l2=i2==0?null:expression.literal[--i2];
|
||||
while(l1!=null || l2!=null) {
|
||||
int c=l1==null?-1:(l2==null?1:l1.compareTo(l2));
|
||||
if(c<0) return -1;
|
||||
else if(c>0) return 1;
|
||||
else {
|
||||
c=coef[i1].compareTo(expression.coef[i2]);
|
||||
if(c<0) return -1;
|
||||
else if(c>0) return 1;
|
||||
l1=i1==0?null:literal[--i1];
|
||||
l2=i2==0?null:expression.literal[--i2];
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public int compareTo(Generic generic) {
|
||||
if(generic instanceof Expression) {
|
||||
return compareTo((Expression)generic);
|
||||
} else if(generic instanceof JSCLInteger || generic instanceof Rational) {
|
||||
return compareTo(valueof(generic));
|
||||
} else {
|
||||
return generic.valueof(this).compareTo(generic);
|
||||
}
|
||||
}
|
||||
|
||||
public static Expression valueOf(Variable variable) {
|
||||
return valueOf(Literal.valueOf(variable));
|
||||
}
|
||||
|
||||
public static Expression valueOf(Literal literal) {
|
||||
return valueOf(literal,JSCLInteger.valueOf(1));
|
||||
}
|
||||
|
||||
public static Expression valueOf(JSCLInteger integer) {
|
||||
return valueOf(Literal.valueOf(),integer);
|
||||
}
|
||||
|
||||
public static Expression valueOf(Literal literal, JSCLInteger integer) {
|
||||
Expression ex=new Expression();
|
||||
ex.init(literal,integer);
|
||||
return ex;
|
||||
}
|
||||
|
||||
void init(Literal lit, JSCLInteger integer) {
|
||||
if(integer.signum()!=0) {
|
||||
init(1);
|
||||
literal[0]=lit;
|
||||
coef[0]=integer;
|
||||
} else init(0);
|
||||
}
|
||||
|
||||
public static Expression valueOf(Rational rational) {
|
||||
Expression ex=new Expression();
|
||||
ex.init(rational);
|
||||
return ex;
|
||||
}
|
||||
|
||||
public static Expression valueOf(String str) throws ParseException {
|
||||
int pos[]=new int[1];
|
||||
Generic a;
|
||||
try {
|
||||
a=(Generic)ExpressionParser.parser.parse(str,pos);
|
||||
} catch (ParseException e) {
|
||||
throw e;
|
||||
}
|
||||
Parser.skipWhitespaces(str,pos);
|
||||
if(pos[0]<str.length()) {
|
||||
throw new ParseException();
|
||||
}
|
||||
Expression ex=new Expression();
|
||||
ex.init(a);
|
||||
return ex;
|
||||
}
|
||||
|
||||
void init(Expression expression) {
|
||||
init(expression.size);
|
||||
System.arraycopy(expression.literal,0,literal,0,size);
|
||||
System.arraycopy(expression.coef,0,coef,0,size);
|
||||
}
|
||||
|
||||
void init(JSCLInteger integer) {
|
||||
init(Literal.valueOf(),integer);
|
||||
}
|
||||
|
||||
void init(Rational rational) {
|
||||
try {
|
||||
init(Literal.valueOf(),rational.integerValue());
|
||||
} catch (NotIntegerException e) {
|
||||
init(Literal.valueOf(rational.variableValue()),JSCLInteger.valueOf(1));
|
||||
}
|
||||
}
|
||||
|
||||
void init(Generic generic) {
|
||||
if(generic instanceof Expression) {
|
||||
init((Expression)generic);
|
||||
} else if(generic instanceof JSCLInteger) {
|
||||
init((JSCLInteger)generic);
|
||||
} else if(generic instanceof Rational) {
|
||||
init((Rational)generic);
|
||||
} else throw new ArithmeticException();
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer buffer=new StringBuffer();
|
||||
if(signum()==0) buffer.append("0");
|
||||
for(int i=0;i<size;i++) {
|
||||
Literal l=literal[i];
|
||||
JSCLInteger en=coef[i];
|
||||
if(en.signum()>0 && i>0) buffer.append("+");
|
||||
if(l.degree()==0) buffer.append(en);
|
||||
else {
|
||||
if(en.abs().compareTo(JSCLInteger.valueOf(1))==0) {
|
||||
if(en.signum()<0) buffer.append("-");
|
||||
} else buffer.append(en).append("*");
|
||||
buffer.append(l);
|
||||
}
|
||||
}
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
public String toJava() {
|
||||
StringBuffer buffer=new StringBuffer();
|
||||
if(signum()==0) buffer.append("JSCLDouble.valueOf(0)");
|
||||
for(int i=0;i<size;i++) {
|
||||
Literal l=literal[i];
|
||||
JSCLInteger en=coef[i];
|
||||
if(i>0) {
|
||||
if(en.signum()<0) {
|
||||
buffer.append(".subtract(");
|
||||
en=(JSCLInteger)en.negate();
|
||||
} else buffer.append(".add(");
|
||||
}
|
||||
if(l.degree()==0) buffer.append(en.toJava());
|
||||
else {
|
||||
if(en.abs().compareTo(JSCLInteger.valueOf(1))==0) {
|
||||
if(en.signum()>0) buffer.append(l.toJava());
|
||||
else if(en.signum()<0) buffer.append(l.toJava()).append(".negate()");
|
||||
} else buffer.append(en.toJava()).append(".multiply(").append(l.toJava()).append(")");
|
||||
}
|
||||
if(i>0) buffer.append(")");
|
||||
}
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
public void toMathML(MathML element, Object data) {
|
||||
MathML e1=element.element("mrow");
|
||||
if(signum()==0) {
|
||||
MathML e2=element.element("mn");
|
||||
e2.appendChild(element.text("0"));
|
||||
e1.appendChild(e2);
|
||||
}
|
||||
for(int i=0;i<size;i++) {
|
||||
Literal l=literal[i];
|
||||
JSCLInteger en=coef[i];
|
||||
if(en.signum()>0 && i>0) {
|
||||
MathML e2=element.element("mo");
|
||||
e2.appendChild(element.text("+"));
|
||||
e1.appendChild(e2);
|
||||
}
|
||||
if(l.degree()==0) separateSign(e1,en);
|
||||
else {
|
||||
if(en.abs().compareTo(JSCLInteger.valueOf(1))==0) {
|
||||
if(en.signum()<0) {
|
||||
MathML e2=element.element("mo");
|
||||
e2.appendChild(element.text("-"));
|
||||
e1.appendChild(e2);
|
||||
}
|
||||
} else separateSign(e1,en);
|
||||
l.toMathML(e1,null);
|
||||
}
|
||||
}
|
||||
element.appendChild(e1);
|
||||
}
|
||||
|
||||
public static void separateSign(MathML element, Generic generic) {
|
||||
if(generic.signum()<0) {
|
||||
MathML e1=element.element("mo");
|
||||
e1.appendChild(element.text("-"));
|
||||
element.appendChild(e1);
|
||||
generic.negate().toMathML(element,null);
|
||||
} else generic.toMathML(element,null);
|
||||
}
|
||||
|
||||
protected Expression newinstance(int n) {
|
||||
return new Expression(n);
|
||||
}
|
||||
}
|
57
src/misc/src/jscl/math/ExpressionVariable.java
Normal file
57
src/misc/src/jscl/math/ExpressionVariable.java
Normal file
@ -0,0 +1,57 @@
|
||||
package jscl.math;
|
||||
|
||||
import jscl.mathml.MathML;
|
||||
|
||||
public class ExpressionVariable extends GenericVariable {
|
||||
public ExpressionVariable(Generic generic) {
|
||||
super(generic);
|
||||
}
|
||||
|
||||
public Generic substitute(Variable variable, Generic generic) {
|
||||
if(isIdentity(variable)) return generic;
|
||||
else return content.substitute(variable,generic);
|
||||
}
|
||||
|
||||
public Generic elementary() {
|
||||
return content.elementary();
|
||||
}
|
||||
|
||||
public Generic simplify() {
|
||||
return content.simplify();
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer buffer=new StringBuffer();
|
||||
buffer.append("(").append(content).append(")");
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
public String toJava() {
|
||||
StringBuffer buffer=new StringBuffer();
|
||||
buffer.append("(").append(content.toJava()).append(")");
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
public void toMathML(MathML element, Object data) {
|
||||
int exponent=data instanceof Integer?((Integer)data).intValue():1;
|
||||
if(exponent==1) bodyToMathML(element);
|
||||
else {
|
||||
MathML e1=element.element("msup");
|
||||
bodyToMathML(e1);
|
||||
MathML e2=element.element("mn");
|
||||
e2.appendChild(element.text(String.valueOf(exponent)));
|
||||
e1.appendChild(e2);
|
||||
element.appendChild(e1);
|
||||
}
|
||||
}
|
||||
|
||||
void bodyToMathML(MathML element) {
|
||||
MathML e1=element.element("mfenced");
|
||||
content.toMathML(e1,null);
|
||||
element.appendChild(e1);
|
||||
}
|
||||
|
||||
protected Variable newinstance() {
|
||||
return new ExpressionVariable(null);
|
||||
}
|
||||
}
|
290
src/misc/src/jscl/math/Factorization.java
Normal file
290
src/misc/src/jscl/math/Factorization.java
Normal file
@ -0,0 +1,290 @@
|
||||
package jscl.math;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import jscl.math.polynomial.Basis;
|
||||
import jscl.math.polynomial.Monomial;
|
||||
import jscl.math.polynomial.Ordering;
|
||||
import jscl.math.polynomial.Polynomial;
|
||||
import jscl.util.ArrayComparator;
|
||||
import jscl.util.ArrayUtils;
|
||||
|
||||
public class Factorization {
|
||||
Polynomial factory;
|
||||
Generic result;
|
||||
|
||||
Factorization(Polynomial factory) {
|
||||
this.factory=factory;
|
||||
}
|
||||
|
||||
public static Generic compute(Generic generic) {
|
||||
try {
|
||||
return GenericVariable.content(factorize(generic.integerValue()));
|
||||
} catch (NotIntegerException e) {
|
||||
Factorization f=new Factorization(Polynomial.factory(generic.variables(),Monomial.iteratorOrdering,-1));
|
||||
f.computeValue(generic);
|
||||
return f.getValue();
|
||||
}
|
||||
}
|
||||
|
||||
static Generic factorize(JSCLInteger integer) {
|
||||
Generic n[]=integer.gcdAndNormalize();
|
||||
Generic s=n[1];
|
||||
Generic a=JSCLInteger.valueOf(1);
|
||||
Generic p=JSCLInteger.valueOf(2);
|
||||
while(s.compareTo(JSCLInteger.valueOf(1))>0) {
|
||||
Generic q[]=s.divideAndRemainder(p);
|
||||
if(q[0].compareTo(p)<0) {
|
||||
p=s;
|
||||
q=s.divideAndRemainder(p);
|
||||
}
|
||||
if(q[1].signum()==0) {
|
||||
a=a.multiply(expression(p,true));
|
||||
s=q[0];
|
||||
} else p=p.add(JSCLInteger.valueOf(1));
|
||||
}
|
||||
return a.multiply(n[0]);
|
||||
}
|
||||
|
||||
void computeValue(Generic generic) {
|
||||
Debug.println("factorization");
|
||||
Polynomial n[]=factory.valueof(generic).gcdAndNormalize();
|
||||
Monomial m=n[1].monomialGcd();
|
||||
Polynomial s=n[1].divide(m);
|
||||
Generic a=JSCLInteger.valueOf(1);
|
||||
Divisor d[]=new Divisor[2];
|
||||
Monomial p[]=new Monomial[2];
|
||||
Monomial q[]=new Monomial[2];
|
||||
d[1]=new Divisor(s.head().monomial());
|
||||
loop: while(d[1].hasNext()) {
|
||||
p[1]=(Monomial)d[1].next();
|
||||
q[1]=d[1].complementary();
|
||||
d[0]=new Divisor(s.tail().monomial());
|
||||
while(d[0].hasNext()) {
|
||||
p[0]=(Monomial)d[0].next();
|
||||
q[0]=d[0].complementary();
|
||||
if(p[1].compareTo(p[0])<=0) continue loop;
|
||||
Debug.println(toString(p)+" * "+toString(q)+" = "+s);
|
||||
if(ArrayComparator.comparator.compare(q,p)<0) {
|
||||
a=a.multiply(expression(s.genericValue()));
|
||||
break loop;
|
||||
} else {
|
||||
Debug.increment();
|
||||
Polynomial r[]=remainder(s,polynomial(s,p),terminator(s));
|
||||
Debug.decrement();
|
||||
if(r[0].signum()==0) {
|
||||
a=a.multiply(expression(r[1].genericValue()));
|
||||
s=r[2];
|
||||
d[1].divide();
|
||||
d[0].divide();
|
||||
continue loop;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
result=a.multiply(n[0].multiply(m).genericValue());
|
||||
}
|
||||
|
||||
static Polynomial[] remainder(Polynomial s, Polynomial p, Generic t[]) {
|
||||
Polynomial zero=s.valueof(JSCLInteger.valueOf(0));
|
||||
Generic a[]=Basis.augment(t,s.remainderUpToCoefficient(p).elements());
|
||||
Variable unk[]=Basis.augmentUnknown(new Variable[] {},p.elements());
|
||||
{
|
||||
Variable u=unk[unk.length-1];
|
||||
System.arraycopy(unk,0,unk,1,unk.length-1);
|
||||
unk[0]=u;
|
||||
}
|
||||
Generic be[][]=Linearization.compute(Basis.compute(a,unk,Monomial.lexicographic,0,Basis.DEGREE).elements(),unk);
|
||||
for(int i=0;i<be.length;i++) {
|
||||
Polynomial r=substitute(p,be[i],unk);
|
||||
try {
|
||||
return new Polynomial[] {zero,r,s.divide(r)};
|
||||
} catch(NotDivisibleException e) {}
|
||||
}
|
||||
return new Polynomial[] {s,zero,zero};
|
||||
}
|
||||
|
||||
static Polynomial substitute(Polynomial p, Generic a[], Variable unk[]) {
|
||||
Generic s[]=new Generic[] {p.genericValue()};
|
||||
return p.valueof(Basis.compute(Basis.augment(a,s),Basis.augmentUnknown(unk,s)).elements()[0]);
|
||||
}
|
||||
|
||||
private static final String ter="t";
|
||||
|
||||
static Polynomial polynomial(Polynomial s, Monomial monomial[]) {
|
||||
Polynomial p=s.valueof(JSCLInteger.valueOf(0));
|
||||
Iterator it=monomial[1].iterator(monomial[0]);
|
||||
for(int i=0;it.hasNext();i++) {
|
||||
Monomial m=(Monomial)it.next();
|
||||
Variable t=it.hasNext()?new TechnicalVariable(ter,new int[] {i}):new TechnicalVariable(ter);
|
||||
p=p.add(p.valueof(m).multiply(t.expressionValue()));
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
static Generic[] terminator(Polynomial polynomial) {
|
||||
Generic t[]=new Generic[2];
|
||||
t[1]=terminator(polynomial.head().coef().abs(),new TechnicalVariable(ter),false);
|
||||
t[0]=terminator(polynomial.tail().coef(),new TechnicalVariable(ter,new int[] {0}),true);
|
||||
return t;
|
||||
}
|
||||
|
||||
static Generic terminator(Generic generic, Variable var, boolean tail) {
|
||||
Generic x=var.expressionValue();
|
||||
Generic a=JSCLInteger.valueOf(1);
|
||||
Iterator it=IntegerDivisor.create(generic.integerValue());
|
||||
while(it.hasNext()) {
|
||||
Generic s=(Generic)it.next();
|
||||
a=a.multiply(x.subtract(s));
|
||||
if(!tail) a=a.multiply(x.add(s));
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
static Generic expression(Generic generic) {
|
||||
return expression(generic,false);
|
||||
}
|
||||
|
||||
static Generic expression(Generic generic, boolean integer) {
|
||||
if(generic.compareTo(JSCLInteger.valueOf(1))==0) return generic;
|
||||
else return GenericVariable.valueOf(generic,integer).expressionValue();
|
||||
}
|
||||
|
||||
static String toString(Monomial monomial[]) {
|
||||
return "{"+monomial[0]+", "+monomial[1]+"}";
|
||||
}
|
||||
|
||||
Generic getValue() {
|
||||
return GenericVariable.content(result,true);
|
||||
}
|
||||
}
|
||||
|
||||
class Linearization {
|
||||
Variable unknown[];
|
||||
List result=new ArrayList();
|
||||
|
||||
Linearization(Variable unknown[]) {
|
||||
this.unknown=unknown;
|
||||
}
|
||||
|
||||
static Generic[][] compute(Generic generic[], Variable unknown[]) {
|
||||
Linearization l=new Linearization(unknown);
|
||||
Debug.println("linearization");
|
||||
Debug.increment();
|
||||
l.process(generic);
|
||||
Debug.decrement();
|
||||
return l.getValue();
|
||||
}
|
||||
|
||||
void process(Generic generic[]) {
|
||||
boolean flag=true;
|
||||
for(int i=0;i<generic.length;i++) {
|
||||
Generic s=generic[i];
|
||||
Variable va[]=s.variables();
|
||||
if(va.length==1) {
|
||||
Variable t=va[0];
|
||||
Polynomial p=Polynomial.factory(t).valueof(s);
|
||||
if(p.degree()>1) {
|
||||
flag=false;
|
||||
Polynomial r[]=linearize(p,t);
|
||||
for(int j=0;j<r.length;j++) {
|
||||
process(Basis.compute(Basis.augment(new Generic[] {r[j].genericValue()},generic),unknown).elements());
|
||||
}
|
||||
}
|
||||
} else flag=false;
|
||||
}
|
||||
if(flag) result.add(generic);
|
||||
}
|
||||
|
||||
static Polynomial[] linearize(Polynomial polynomial, Variable variable) {
|
||||
List l=new ArrayList();
|
||||
Generic x=variable.expressionValue();
|
||||
Polynomial s=polynomial;
|
||||
try {
|
||||
Polynomial r=s.valueof(x);
|
||||
s=s.divide(r);
|
||||
l.add(r);
|
||||
while(true) s=s.divide(r);
|
||||
} catch (NotDivisibleException e) {}
|
||||
IntegerDivisor d[]=new IntegerDivisor[2];
|
||||
Generic p[]=new Generic[2];
|
||||
Generic q[]=new Generic[2];
|
||||
d[1]=IntegerDivisor.create(JSCLInteger.valueOf(1));
|
||||
loop: while(d[1].hasNext()) {
|
||||
p[1]=(Generic)d[1].next();
|
||||
q[1]=d[1].integer(d[1].complementary());
|
||||
d[0]=IntegerDivisor.create(s.tail().coef().integerValue());
|
||||
while(d[0].hasNext()) {
|
||||
p[0]=(Generic)d[0].next();
|
||||
q[0]=d[0].integer(d[0].complementary());
|
||||
if(ArrayComparator.comparator.compare(q,p)<0) break loop;
|
||||
for(int i=0;i<2;i++) {
|
||||
Polynomial r=s.valueof(i==0?p[1].multiply(x).subtract(p[0]):p[1].multiply(x).add(p[0]));
|
||||
for(boolean flag=true;true;flag=false) {
|
||||
try { s=s.divide(r); } catch (NotDivisibleException e) { break; }
|
||||
d[1].divide();
|
||||
d[0].divide();
|
||||
if(flag) l.add(r);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return (Polynomial[])ArrayUtils.toArray(l,new Polynomial[l.size()]);
|
||||
}
|
||||
|
||||
Generic[][] getValue() {
|
||||
return (Generic[][])ArrayUtils.toArray(result,new Generic[result.size()][]);
|
||||
}
|
||||
}
|
||||
|
||||
class Divisor implements Iterator {
|
||||
Monomial monomial;
|
||||
Monomial current;
|
||||
Iterator iterator;
|
||||
|
||||
Divisor(Monomial monomial) {
|
||||
this.monomial=monomial;
|
||||
iterator=monomial.divisor();
|
||||
}
|
||||
|
||||
Monomial complementary() {
|
||||
return monomial.divide(current);
|
||||
}
|
||||
|
||||
void divide() {
|
||||
monomial=complementary();
|
||||
iterator=monomial.divisor(current);
|
||||
}
|
||||
|
||||
public boolean hasNext() {
|
||||
return iterator.hasNext();
|
||||
}
|
||||
|
||||
public Object next() {
|
||||
return current=(Monomial)iterator.next();
|
||||
}
|
||||
|
||||
public void remove() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
|
||||
class IntegerDivisor extends Divisor {
|
||||
IntegerDivisor(Generic generic, Variable unknown[], Ordering ordering) {
|
||||
super(Polynomial.factory(unknown,ordering).valueof(generic).head().monomial());
|
||||
}
|
||||
|
||||
public Object next() {
|
||||
return integer((Monomial)super.next());
|
||||
}
|
||||
|
||||
Generic integer(Monomial monomial) {
|
||||
return Expression.valueOf(Literal.valueOf(monomial)).expand();
|
||||
}
|
||||
|
||||
static IntegerDivisor create(JSCLInteger integer) {
|
||||
Generic a=Factorization.factorize(integer);
|
||||
return new IntegerDivisor(a,a.variables(),Monomial.iteratorOrdering);
|
||||
}
|
||||
}
|
3
src/misc/src/jscl/math/Field.java
Normal file
3
src/misc/src/jscl/math/Field.java
Normal file
@ -0,0 +1,3 @@
|
||||
package jscl.math;
|
||||
|
||||
public interface Field {}
|
138
src/misc/src/jscl/math/Generic.java
Normal file
138
src/misc/src/jscl/math/Generic.java
Normal file
@ -0,0 +1,138 @@
|
||||
package jscl.math;
|
||||
|
||||
import jscl.mathml.MathML;
|
||||
|
||||
public abstract class Generic implements Arithmetic, Comparable {
|
||||
public abstract Generic add(Generic generic);
|
||||
|
||||
public Generic subtract(Generic generic) {
|
||||
return add(generic.negate());
|
||||
}
|
||||
|
||||
public abstract Generic multiply(Generic generic);
|
||||
|
||||
public boolean multiple(Generic generic) throws ArithmeticException {
|
||||
return remainder(generic).signum()==0;
|
||||
}
|
||||
|
||||
public abstract Generic divide(Generic generic) throws ArithmeticException;
|
||||
|
||||
public Arithmetic add(Arithmetic arithmetic) {
|
||||
return add((Generic)arithmetic);
|
||||
}
|
||||
|
||||
public Arithmetic subtract(Arithmetic arithmetic) {
|
||||
return subtract((Generic)arithmetic);
|
||||
}
|
||||
|
||||
public Arithmetic multiply(Arithmetic arithmetic) {
|
||||
return multiply((Generic)arithmetic);
|
||||
}
|
||||
|
||||
public Arithmetic divide(Arithmetic arithmetic) throws ArithmeticException {
|
||||
return divide((Generic)arithmetic);
|
||||
}
|
||||
|
||||
public Generic[] divideAndRemainder(Generic generic) throws ArithmeticException {
|
||||
try {
|
||||
return new Generic[] {divide(generic),JSCLInteger.valueOf(0)};
|
||||
} catch (NotDivisibleException e) {
|
||||
return new Generic[] {JSCLInteger.valueOf(0),this};
|
||||
}
|
||||
}
|
||||
|
||||
public Generic remainder(Generic generic) throws ArithmeticException {
|
||||
return divideAndRemainder(generic)[1];
|
||||
}
|
||||
|
||||
public Generic inverse() {
|
||||
return JSCLInteger.valueOf(1).divide(this);
|
||||
}
|
||||
|
||||
public abstract Generic gcd(Generic generic);
|
||||
|
||||
public Generic scm(Generic generic) {
|
||||
return divide(gcd(generic)).multiply(generic);
|
||||
}
|
||||
|
||||
public abstract Generic gcd();
|
||||
|
||||
public Generic[] gcdAndNormalize() {
|
||||
Generic gcd=gcd();
|
||||
if(gcd.signum()==0) return new Generic[] {gcd,this};
|
||||
if(gcd.signum()!=signum()) gcd=gcd.negate();
|
||||
return new Generic[] {gcd,divide(gcd)};
|
||||
}
|
||||
|
||||
public Generic normalize() {
|
||||
return gcdAndNormalize()[1];
|
||||
}
|
||||
|
||||
public Generic pow(int exponent) {
|
||||
Generic a=JSCLInteger.valueOf(1);
|
||||
for(int i=0;i<exponent;i++) a=a.multiply(this);
|
||||
return a;
|
||||
}
|
||||
|
||||
public Generic abs() {
|
||||
return signum()<0?negate():this;
|
||||
}
|
||||
|
||||
public abstract Generic negate();
|
||||
public abstract int signum();
|
||||
public abstract int degree();
|
||||
|
||||
// public abstract Generic mod(Generic generic);
|
||||
// public abstract Generic modPow(Generic exponent, Generic generic);
|
||||
// public abstract Generic modInverse(Generic generic);
|
||||
// public abstract boolean isProbablePrime(int certainty);
|
||||
public abstract Generic antiderivative(Variable variable) throws NotIntegrableException;
|
||||
public abstract Generic derivative(Variable variable);
|
||||
public abstract Generic substitute(Variable variable, Generic generic);
|
||||
public abstract Generic expand();
|
||||
public abstract Generic factorize();
|
||||
public abstract Generic elementary();
|
||||
public abstract Generic simplify();
|
||||
public abstract Generic numeric();
|
||||
public abstract Generic valueof(Generic generic);
|
||||
public abstract Generic[] sumValue();
|
||||
public abstract Generic[] productValue() throws NotProductException;
|
||||
public abstract Power powerValue() throws NotPowerException;
|
||||
public abstract Expression expressionValue() throws NotExpressionException;
|
||||
public abstract JSCLInteger integerValue() throws NotIntegerException;
|
||||
public abstract Variable variableValue() throws NotVariableException;
|
||||
public abstract Variable[] variables();
|
||||
public abstract boolean isPolynomial(Variable variable);
|
||||
public abstract boolean isConstant(Variable variable);
|
||||
|
||||
public boolean isIdentity(Variable variable) {
|
||||
try {
|
||||
return variableValue().isIdentity(variable);
|
||||
} catch (NotVariableException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public abstract int compareTo(Generic generic);
|
||||
|
||||
public int compareTo(Object o) {
|
||||
return compareTo((Generic)o);
|
||||
}
|
||||
|
||||
public boolean equals(Object obj) {
|
||||
if(obj instanceof Generic) {
|
||||
return compareTo((Generic)obj)==0;
|
||||
} else return false;
|
||||
}
|
||||
|
||||
public abstract String toJava();
|
||||
|
||||
public String toMathML() {
|
||||
MathML document=new MathML("math","-//W3C//DTD MathML 2.0//EN","http://www.w3.org/TR/MathML2/dtd/mathml2.dtd");
|
||||
MathML e=document.element("math");
|
||||
toMathML(e,null);
|
||||
return e.toString();
|
||||
}
|
||||
|
||||
public abstract void toMathML(MathML element, Object data);
|
||||
}
|
105
src/misc/src/jscl/math/GenericVariable.java
Normal file
105
src/misc/src/jscl/math/GenericVariable.java
Normal file
@ -0,0 +1,105 @@
|
||||
package jscl.math;
|
||||
|
||||
import jscl.mathml.MathML;
|
||||
|
||||
public abstract class GenericVariable extends Variable {
|
||||
Generic content;
|
||||
|
||||
GenericVariable(Generic generic) {
|
||||
super("");
|
||||
content=generic;
|
||||
}
|
||||
|
||||
public static Generic content(Generic generic) {
|
||||
return content(generic,false);
|
||||
}
|
||||
|
||||
public static Generic content(Generic generic, boolean expression) {
|
||||
try {
|
||||
Variable v=generic.variableValue();
|
||||
if(expression) {
|
||||
if(v instanceof ExpressionVariable) generic=((ExpressionVariable)v).content;
|
||||
} else {
|
||||
if(v instanceof GenericVariable) generic=((GenericVariable)v).content;
|
||||
}
|
||||
} catch (NotVariableException e) {}
|
||||
return generic;
|
||||
}
|
||||
|
||||
public Generic antiderivative(Variable variable) throws NotIntegrableException {
|
||||
return content.antiderivative(variable);
|
||||
}
|
||||
|
||||
public Generic derivative(Variable variable) {
|
||||
return content.derivative(variable);
|
||||
}
|
||||
|
||||
public Generic substitute(Variable variable, Generic generic) {
|
||||
GenericVariable v=(GenericVariable)newinstance();
|
||||
v.content=content.substitute(variable,generic);
|
||||
if(v.isIdentity(variable)) return generic;
|
||||
else return v.expressionValue();
|
||||
}
|
||||
|
||||
public Generic expand() {
|
||||
return content.expand();
|
||||
}
|
||||
|
||||
public Generic factorize() {
|
||||
GenericVariable v=(GenericVariable)newinstance();
|
||||
v.content=content.factorize();
|
||||
return v.expressionValue();
|
||||
}
|
||||
|
||||
public Generic elementary() {
|
||||
GenericVariable v=(GenericVariable)newinstance();
|
||||
v.content=content.elementary();
|
||||
return v.expressionValue();
|
||||
}
|
||||
|
||||
public Generic simplify() {
|
||||
GenericVariable v=(GenericVariable)newinstance();
|
||||
v.content=content.simplify();
|
||||
return v.expressionValue();
|
||||
}
|
||||
|
||||
public Generic numeric() {
|
||||
return content.numeric();
|
||||
}
|
||||
|
||||
public boolean isConstant(Variable variable) {
|
||||
return content.isConstant(variable);
|
||||
}
|
||||
|
||||
public int compareTo(Variable variable) {
|
||||
if(this==variable) return 0;
|
||||
int c=comparator.compare(this,variable);
|
||||
if(c<0) return -1;
|
||||
else if(c>0) return 1;
|
||||
else {
|
||||
GenericVariable v=(GenericVariable)variable;
|
||||
return content.compareTo(v.content);
|
||||
}
|
||||
}
|
||||
|
||||
public static GenericVariable valueOf(Generic generic) {
|
||||
return valueOf(generic,false);
|
||||
}
|
||||
|
||||
public static GenericVariable valueOf(Generic generic, boolean integer) {
|
||||
if(integer) return new IntegerVariable(generic);
|
||||
else return new ExpressionVariable(generic);
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return content.toString();
|
||||
}
|
||||
|
||||
public String toJava() {
|
||||
return content.toJava();
|
||||
}
|
||||
|
||||
public void toMathML(MathML element, Object data) {
|
||||
content.toMathML(element,data);
|
||||
}
|
||||
}
|
57
src/misc/src/jscl/math/IntegerVariable.java
Normal file
57
src/misc/src/jscl/math/IntegerVariable.java
Normal file
@ -0,0 +1,57 @@
|
||||
package jscl.math;
|
||||
|
||||
import jscl.mathml.MathML;
|
||||
|
||||
class IntegerVariable extends GenericVariable {
|
||||
IntegerVariable(Generic generic) {
|
||||
super(generic);
|
||||
}
|
||||
|
||||
public Generic substitute(Variable variable, Generic generic) {
|
||||
if(isIdentity(variable)) return generic;
|
||||
else return content.substitute(variable,generic);
|
||||
}
|
||||
|
||||
public Generic elementary() {
|
||||
return content.elementary();
|
||||
}
|
||||
|
||||
public Generic simplify() {
|
||||
return content.simplify();
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer buffer=new StringBuffer();
|
||||
buffer.append("(").append(content).append(")");
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
public String toJava() {
|
||||
StringBuffer buffer=new StringBuffer();
|
||||
buffer.append("(").append(content.toJava()).append(")");
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
public void toMathML(MathML element, Object data) {
|
||||
int exponent=data instanceof Integer?((Integer)data).intValue():1;
|
||||
if(exponent==1) bodyToMathML(element);
|
||||
else {
|
||||
MathML e1=element.element("msup");
|
||||
bodyToMathML(e1);
|
||||
MathML e2=element.element("mn");
|
||||
e2.appendChild(element.text(String.valueOf(exponent)));
|
||||
e1.appendChild(e2);
|
||||
element.appendChild(e1);
|
||||
}
|
||||
}
|
||||
|
||||
void bodyToMathML(MathML element) {
|
||||
MathML e1=element.element("mfenced");
|
||||
content.toMathML(e1,null);
|
||||
element.appendChild(e1);
|
||||
}
|
||||
|
||||
protected Variable newinstance() {
|
||||
return new IntegerVariable(null);
|
||||
}
|
||||
}
|
16
src/misc/src/jscl/math/JSCLBoolean.java
Normal file
16
src/misc/src/jscl/math/JSCLBoolean.java
Normal file
@ -0,0 +1,16 @@
|
||||
package jscl.math;
|
||||
|
||||
public class JSCLBoolean extends ModularInteger {
|
||||
public static final JSCLBoolean factory=new JSCLBoolean(0);
|
||||
|
||||
public JSCLBoolean(long content) {
|
||||
super(content,2);
|
||||
}
|
||||
|
||||
protected ModularInteger newinstance(long content) {
|
||||
return content%2==0?zero:one;
|
||||
}
|
||||
|
||||
private static final JSCLBoolean zero=factory;
|
||||
private static final JSCLBoolean one=new JSCLBoolean(1);
|
||||
}
|
330
src/misc/src/jscl/math/JSCLInteger.java
Normal file
330
src/misc/src/jscl/math/JSCLInteger.java
Normal file
@ -0,0 +1,330 @@
|
||||
package jscl.math;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import jscl.mathml.MathML;
|
||||
|
||||
public final class JSCLInteger extends Generic {
|
||||
public static final JSCLInteger factory=new JSCLInteger(BigInteger.valueOf(0));
|
||||
final BigInteger content;
|
||||
|
||||
public JSCLInteger(BigInteger content) {
|
||||
this.content=content;
|
||||
}
|
||||
|
||||
public BigInteger content() {
|
||||
return content;
|
||||
}
|
||||
|
||||
public JSCLInteger add(JSCLInteger integer) {
|
||||
return new JSCLInteger(content.add(integer.content));
|
||||
}
|
||||
|
||||
public Generic add(Generic generic) {
|
||||
if(generic instanceof JSCLInteger) {
|
||||
return add((JSCLInteger)generic);
|
||||
} else {
|
||||
return generic.valueof(this).add(generic);
|
||||
}
|
||||
}
|
||||
|
||||
public JSCLInteger subtract(JSCLInteger integer) {
|
||||
return new JSCLInteger(content.subtract(integer.content));
|
||||
}
|
||||
|
||||
public Generic subtract(Generic generic) {
|
||||
if(generic instanceof JSCLInteger) {
|
||||
return subtract((JSCLInteger)generic);
|
||||
} else {
|
||||
return generic.valueof(this).subtract(generic);
|
||||
}
|
||||
}
|
||||
|
||||
public JSCLInteger multiply(JSCLInteger integer) {
|
||||
return new JSCLInteger(content.multiply(integer.content));
|
||||
}
|
||||
|
||||
public Generic multiply(Generic generic) {
|
||||
if(generic instanceof JSCLInteger) {
|
||||
return multiply((JSCLInteger)generic);
|
||||
} else {
|
||||
return generic.multiply(this);
|
||||
}
|
||||
}
|
||||
|
||||
public JSCLInteger divide(JSCLInteger integer) throws ArithmeticException {
|
||||
JSCLInteger e[]=divideAndRemainder(integer);
|
||||
if(e[1].signum()==0) return e[0];
|
||||
else throw new NotDivisibleException();
|
||||
}
|
||||
|
||||
public Generic divide(Generic generic) throws ArithmeticException {
|
||||
if(generic instanceof JSCLInteger) {
|
||||
return divide((JSCLInteger)generic);
|
||||
} else {
|
||||
return generic.valueof(this).divide(generic);
|
||||
}
|
||||
}
|
||||
|
||||
public JSCLInteger[] divideAndRemainder(JSCLInteger integer) throws ArithmeticException {
|
||||
BigInteger b[]=content.divideAndRemainder(integer.content);
|
||||
return new JSCLInteger[] {new JSCLInteger(b[0]),new JSCLInteger(b[1])};
|
||||
}
|
||||
|
||||
public Generic[] divideAndRemainder(Generic generic) throws ArithmeticException {
|
||||
if(generic instanceof JSCLInteger) {
|
||||
return divideAndRemainder((JSCLInteger)generic);
|
||||
} else {
|
||||
return generic.valueof(this).divideAndRemainder(generic);
|
||||
}
|
||||
}
|
||||
|
||||
public JSCLInteger remainder(JSCLInteger integer) throws ArithmeticException {
|
||||
return new JSCLInteger(content.remainder(integer.content));
|
||||
}
|
||||
|
||||
public Generic remainder(Generic generic) throws ArithmeticException {
|
||||
if(generic instanceof JSCLInteger) {
|
||||
return remainder((JSCLInteger)generic);
|
||||
} else {
|
||||
return generic.valueof(this).remainder(generic);
|
||||
}
|
||||
}
|
||||
|
||||
public JSCLInteger gcd(JSCLInteger integer) {
|
||||
return new JSCLInteger(content.gcd(integer.content));
|
||||
}
|
||||
|
||||
public Generic gcd(Generic generic) {
|
||||
if(generic instanceof JSCLInteger) {
|
||||
return gcd((JSCLInteger)generic);
|
||||
} else {
|
||||
return generic.valueof(this).gcd(generic);
|
||||
}
|
||||
}
|
||||
|
||||
public Generic gcd() {
|
||||
return new JSCLInteger(BigInteger.valueOf(signum()));
|
||||
}
|
||||
|
||||
public Generic pow(int exponent) {
|
||||
return new JSCLInteger(content.pow(exponent));
|
||||
}
|
||||
|
||||
public Generic negate() {
|
||||
return new JSCLInteger(content.negate());
|
||||
}
|
||||
|
||||
public int signum() {
|
||||
return content.signum();
|
||||
}
|
||||
|
||||
public int degree() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public JSCLInteger mod(JSCLInteger integer) {
|
||||
return new JSCLInteger(content.mod(integer.content));
|
||||
}
|
||||
|
||||
public JSCLInteger modPow(JSCLInteger exponent, JSCLInteger integer) {
|
||||
return new JSCLInteger(content.modPow(exponent.content,integer.content));
|
||||
}
|
||||
|
||||
public JSCLInteger modInverse(JSCLInteger integer) {
|
||||
return new JSCLInteger(content.modInverse(integer.content));
|
||||
}
|
||||
|
||||
public JSCLInteger phi() {
|
||||
if(signum()==0) return this;
|
||||
Generic a=factorize();
|
||||
Generic p[]=a.productValue();
|
||||
Generic s=JSCLInteger.valueOf(1);
|
||||
for(int i=0;i<p.length;i++) {
|
||||
Power o=p[i].powerValue();
|
||||
Generic q=o.value(true);
|
||||
int c=o.exponent();
|
||||
s=s.multiply(q.subtract(JSCLInteger.valueOf(1)).multiply(q.pow(c-1)));
|
||||
}
|
||||
return s.integerValue();
|
||||
}
|
||||
|
||||
public JSCLInteger[] primitiveRoots() {
|
||||
JSCLInteger phi=phi();
|
||||
Generic a=phi.factorize();
|
||||
Generic p[]=a.productValue();
|
||||
JSCLInteger d[]=new JSCLInteger[p.length];
|
||||
for(int i=0;i<p.length;i++) {
|
||||
d[i]=phi.divide(p[i].powerValue().value(true).integerValue());
|
||||
}
|
||||
int k=0;
|
||||
JSCLInteger n=this;
|
||||
JSCLInteger m=JSCLInteger.valueOf(1);
|
||||
JSCLInteger r[]=new JSCLInteger[phi.phi().intValue()];
|
||||
while(m.compareTo(n)<0) {
|
||||
boolean b=m.gcd(n).compareTo(JSCLInteger.valueOf(1))==0;
|
||||
for(int i=0;i<d.length;i++) {
|
||||
b=b && m.modPow(d[i],n).compareTo(JSCLInteger.valueOf(1))>0;
|
||||
}
|
||||
if(b) r[k++]=m;
|
||||
m=m.add(JSCLInteger.valueOf(1));
|
||||
}
|
||||
return k>0?r:new JSCLInteger[0];
|
||||
}
|
||||
|
||||
public JSCLInteger sqrt() {
|
||||
return nthrt(2);
|
||||
}
|
||||
|
||||
public JSCLInteger nthrt(int n) {
|
||||
// return JSCLInteger.valueOf((int)Math.pow((double)intValue(),1./n));
|
||||
if(signum()==0) return JSCLInteger.valueOf(0);
|
||||
else if(signum()<0) {
|
||||
if(n%2==0) throw new ArithmeticException();
|
||||
else return (JSCLInteger)((JSCLInteger)negate()).nthrt(n).negate();
|
||||
} else {
|
||||
Generic x0;
|
||||
Generic x=this;
|
||||
do {
|
||||
x0=x;
|
||||
x=divideAndRemainder(x.pow(n-1))[0].add(x.multiply(JSCLInteger.valueOf(n-1))).divideAndRemainder(JSCLInteger.valueOf(n))[0];
|
||||
} while(x.compareTo(x0)<0);
|
||||
return x0.integerValue();
|
||||
}
|
||||
}
|
||||
|
||||
public Generic antiderivative(Variable variable) throws NotIntegrableException {
|
||||
return multiply(variable.expressionValue());
|
||||
}
|
||||
|
||||
public Generic derivative(Variable variable) {
|
||||
return JSCLInteger.valueOf(0);
|
||||
}
|
||||
|
||||
public Generic substitute(Variable variable, Generic generic) {
|
||||
return this;
|
||||
}
|
||||
|
||||
public Generic expand() {
|
||||
return this;
|
||||
}
|
||||
|
||||
public Generic factorize() {
|
||||
return Factorization.compute(this);
|
||||
}
|
||||
|
||||
public Generic elementary() {
|
||||
return this;
|
||||
}
|
||||
|
||||
public Generic simplify() {
|
||||
return this;
|
||||
}
|
||||
|
||||
public Generic numeric() {
|
||||
return new NumericWrapper(this);
|
||||
}
|
||||
|
||||
public Generic valueof(Generic generic) {
|
||||
return new JSCLInteger(((JSCLInteger)generic).content);
|
||||
}
|
||||
|
||||
public Generic[] sumValue() {
|
||||
if(content.signum()==0) return new Generic[0];
|
||||
else return new Generic[] {this};
|
||||
}
|
||||
|
||||
public Generic[] productValue() throws NotProductException {
|
||||
if(content.compareTo(BigInteger.valueOf(1))==0) return new Generic[0];
|
||||
else return new Generic[] {this};
|
||||
}
|
||||
|
||||
public Power powerValue() throws NotPowerException {
|
||||
if(content.signum()<0) throw new NotPowerException();
|
||||
else return new Power(this,1);
|
||||
}
|
||||
|
||||
public Expression expressionValue() throws NotExpressionException {
|
||||
return Expression.valueOf(this);
|
||||
}
|
||||
|
||||
public JSCLInteger integerValue() throws NotIntegerException {
|
||||
return this;
|
||||
}
|
||||
|
||||
public Variable variableValue() throws NotVariableException {
|
||||
throw new NotVariableException();
|
||||
}
|
||||
|
||||
public Variable[] variables() {
|
||||
return new Variable[0];
|
||||
}
|
||||
|
||||
public boolean isPolynomial(Variable variable) {
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean isConstant(Variable variable) {
|
||||
return true;
|
||||
}
|
||||
|
||||
public int intValue() {
|
||||
return content.intValue();
|
||||
}
|
||||
|
||||
public int compareTo(JSCLInteger integer) {
|
||||
return content.compareTo(integer.content);
|
||||
}
|
||||
|
||||
public int compareTo(Generic generic) {
|
||||
if(generic instanceof JSCLInteger) {
|
||||
return compareTo((JSCLInteger)generic);
|
||||
} else {
|
||||
return generic.valueof(this).compareTo(generic);
|
||||
}
|
||||
}
|
||||
|
||||
private static final JSCLInteger ZERO=new JSCLInteger(BigInteger.valueOf(0));
|
||||
private static final JSCLInteger ONE=new JSCLInteger(BigInteger.valueOf(1));
|
||||
|
||||
public static JSCLInteger valueOf(long val) {
|
||||
switch((int)val) {
|
||||
case 0:
|
||||
return ZERO;
|
||||
case 1:
|
||||
return ONE;
|
||||
default:
|
||||
return new JSCLInteger(BigInteger.valueOf(val));
|
||||
}
|
||||
}
|
||||
|
||||
public static JSCLInteger valueOf(String str) {
|
||||
return new JSCLInteger(new BigInteger(str));
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return content.toString();
|
||||
}
|
||||
|
||||
public String toJava() {
|
||||
return "JSCLDouble.valueOf("+content+")";
|
||||
}
|
||||
|
||||
public void toMathML(MathML element, Object data) {
|
||||
int exponent=data instanceof Integer?((Integer)data).intValue():1;
|
||||
if(exponent==1) bodyToMathML(element);
|
||||
else {
|
||||
MathML e1=element.element("msup");
|
||||
bodyToMathML(e1);
|
||||
MathML e2=element.element("mn");
|
||||
e2.appendChild(element.text(String.valueOf(exponent)));
|
||||
e1.appendChild(e2);
|
||||
element.appendChild(e1);
|
||||
}
|
||||
}
|
||||
|
||||
void bodyToMathML(MathML element) {
|
||||
MathML e1=element.element("mn");
|
||||
e1.appendChild(element.text(String.valueOf(content)));
|
||||
element.appendChild(e1);
|
||||
}
|
||||
}
|
476
src/misc/src/jscl/math/JSCLVector.java
Normal file
476
src/misc/src/jscl/math/JSCLVector.java
Normal file
@ -0,0 +1,476 @@
|
||||
package jscl.math;
|
||||
|
||||
import jscl.math.function.Conjugate;
|
||||
import jscl.math.function.Frac;
|
||||
import jscl.mathml.MathML;
|
||||
import jscl.util.ArrayComparator;
|
||||
|
||||
public class JSCLVector extends Generic {
|
||||
protected final Generic element[];
|
||||
protected final int n;
|
||||
|
||||
public JSCLVector(Generic element[]) {
|
||||
this.element=element;
|
||||
n=element.length;
|
||||
}
|
||||
|
||||
public Generic[] elements() {
|
||||
return element;
|
||||
}
|
||||
|
||||
public JSCLVector add(JSCLVector vector) {
|
||||
JSCLVector v=(JSCLVector)newinstance();
|
||||
for(int i=0;i<n;i++) v.element[i]=element[i].add(vector.element[i]);
|
||||
return v;
|
||||
}
|
||||
|
||||
public Generic add(Generic generic) {
|
||||
if(generic instanceof JSCLVector) {
|
||||
return add((JSCLVector)generic);
|
||||
} else {
|
||||
return add(valueof(generic));
|
||||
}
|
||||
}
|
||||
|
||||
public JSCLVector subtract(JSCLVector vector) {
|
||||
JSCLVector v=(JSCLVector)newinstance();
|
||||
for(int i=0;i<n;i++) v.element[i]=element[i].subtract(vector.element[i]);
|
||||
return v;
|
||||
}
|
||||
|
||||
public Generic subtract(Generic generic) {
|
||||
if(generic instanceof JSCLVector) {
|
||||
return subtract((JSCLVector)generic);
|
||||
} else {
|
||||
return subtract(valueof(generic));
|
||||
}
|
||||
}
|
||||
|
||||
public Generic multiply(Generic generic) {
|
||||
if(generic instanceof JSCLVector) {
|
||||
return scalarProduct((JSCLVector)generic);
|
||||
} else if(generic instanceof Matrix) {
|
||||
return ((Matrix)generic).transpose().multiply(this);
|
||||
} else {
|
||||
JSCLVector v=(JSCLVector)newinstance();
|
||||
for(int i=0;i<n;i++) v.element[i]=element[i].multiply(generic);
|
||||
return v;
|
||||
}
|
||||
}
|
||||
|
||||
public Generic divide(Generic generic) throws ArithmeticException {
|
||||
if(generic instanceof JSCLVector) {
|
||||
throw new ArithmeticException();
|
||||
} else if(generic instanceof Matrix) {
|
||||
return multiply(((Matrix)generic).inverse());
|
||||
} else {
|
||||
JSCLVector v=(JSCLVector)newinstance();
|
||||
for(int i=0;i<n;i++) {
|
||||
try {
|
||||
v.element[i]=element[i].divide(generic);
|
||||
} catch (NotDivisibleException e) {
|
||||
v.element[i]=new Frac(element[i],generic).evaluate();
|
||||
}
|
||||
}
|
||||
return v;
|
||||
}
|
||||
}
|
||||
|
||||
public Generic gcd(Generic generic) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public Generic gcd() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public Generic negate() {
|
||||
JSCLVector v=(JSCLVector)newinstance();
|
||||
for(int i=0;i<n;i++) v.element[i]=element[i].negate();
|
||||
return v;
|
||||
}
|
||||
|
||||
public int signum() {
|
||||
for(int i=0;i<n;i++) {
|
||||
int c=element[i].signum();
|
||||
if(c<0) return -1;
|
||||
else if(c>0) return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public int degree() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public Generic antiderivative(Variable variable) throws NotIntegrableException {
|
||||
JSCLVector v=(JSCLVector)newinstance();
|
||||
for(int i=0;i<n;i++) v.element[i]=element[i].antiderivative(variable);
|
||||
return v;
|
||||
}
|
||||
|
||||
public Generic derivative(Variable variable) {
|
||||
JSCLVector v=(JSCLVector)newinstance();
|
||||
for(int i=0;i<n;i++) v.element[i]=element[i].derivative(variable);
|
||||
return v;
|
||||
}
|
||||
|
||||
public Generic substitute(Variable variable, Generic generic) {
|
||||
JSCLVector v=(JSCLVector)newinstance();
|
||||
for(int i=0;i<n;i++) v.element[i]=element[i].substitute(variable,generic);
|
||||
return v;
|
||||
}
|
||||
|
||||
public Generic expand() {
|
||||
JSCLVector v=(JSCLVector)newinstance();
|
||||
for(int i=0;i<n;i++) v.element[i]=element[i].expand();
|
||||
return v;
|
||||
}
|
||||
|
||||
public Generic factorize() {
|
||||
JSCLVector v=(JSCLVector)newinstance();
|
||||
for(int i=0;i<n;i++) v.element[i]=element[i].factorize();
|
||||
return v;
|
||||
}
|
||||
|
||||
public Generic elementary() {
|
||||
JSCLVector v=(JSCLVector)newinstance();
|
||||
for(int i=0;i<n;i++) v.element[i]=element[i].elementary();
|
||||
return v;
|
||||
}
|
||||
|
||||
public Generic simplify() {
|
||||
JSCLVector v=(JSCLVector)newinstance();
|
||||
for(int i=0;i<n;i++) v.element[i]=element[i].simplify();
|
||||
return v;
|
||||
}
|
||||
|
||||
public Generic numeric() {
|
||||
return new NumericWrapper(this);
|
||||
}
|
||||
|
||||
public Generic valueof(Generic generic) {
|
||||
if(generic instanceof JSCLVector || generic instanceof Matrix) {
|
||||
throw new ArithmeticException();
|
||||
} else {
|
||||
JSCLVector v=(JSCLVector)unity(n).multiply(generic);
|
||||
return newinstance(v.element);
|
||||
}
|
||||
}
|
||||
|
||||
public Generic[] sumValue() {
|
||||
return new Generic[] {this};
|
||||
}
|
||||
|
||||
public Generic[] productValue() throws NotProductException {
|
||||
return new Generic[] {this};
|
||||
}
|
||||
|
||||
public Power powerValue() throws NotPowerException {
|
||||
return new Power(this,1);
|
||||
}
|
||||
|
||||
public Expression expressionValue() throws NotExpressionException {
|
||||
throw new NotExpressionException();
|
||||
}
|
||||
|
||||
public JSCLInteger integerValue() throws NotIntegerException {
|
||||
throw new NotIntegerException();
|
||||
}
|
||||
|
||||
public Variable variableValue() throws NotVariableException {
|
||||
throw new NotVariableException();
|
||||
}
|
||||
|
||||
public Variable[] variables() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean isPolynomial(Variable variable) {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isConstant(Variable variable) {
|
||||
return false;
|
||||
}
|
||||
|
||||
public Generic magnitude2() {
|
||||
return scalarProduct(this);
|
||||
}
|
||||
|
||||
public Generic scalarProduct(JSCLVector vector) {
|
||||
Generic a=JSCLInteger.valueOf(0);
|
||||
for(int i=0;i<n;i++) {
|
||||
a=a.add(element[i].multiply(vector.element[i]));
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
public JSCLVector vectorProduct(JSCLVector vector) {
|
||||
JSCLVector v=(JSCLVector)newinstance();
|
||||
Generic m[][]={
|
||||
{JSCLInteger.valueOf(0),element[2].negate(),element[1]},
|
||||
{element[2],JSCLInteger.valueOf(0),element[0].negate()},
|
||||
{element[1].negate(),element[0],JSCLInteger.valueOf(0)}
|
||||
};
|
||||
JSCLVector v2=(JSCLVector)new Matrix(m).multiply(vector);
|
||||
for(int i=0;i<n;i++) v.element[i]=i<v2.n?v2.element[i]:JSCLInteger.valueOf(0);
|
||||
return v;
|
||||
}
|
||||
|
||||
public JSCLVector complexProduct(JSCLVector vector) {
|
||||
return product(new Clifford(0,1).operator(),vector);
|
||||
}
|
||||
|
||||
public JSCLVector quaternionProduct(JSCLVector vector) {
|
||||
return product(new Clifford(0,2).operator(),vector);
|
||||
}
|
||||
|
||||
public JSCLVector geometricProduct(JSCLVector vector, int algebra[]) {
|
||||
return product(new Clifford(algebra==null?new int[] {Clifford.log2e(n),0}:algebra).operator(),vector);
|
||||
}
|
||||
|
||||
JSCLVector product(int product[][], JSCLVector vector) {
|
||||
JSCLVector v=(JSCLVector)newinstance();
|
||||
for(int i=0;i<n;i++) v.element[i]=JSCLInteger.valueOf(0);
|
||||
for(int i=0;i<n;i++) {
|
||||
for(int j=0;j<n;j++) {
|
||||
Generic a=element[i].multiply(vector.element[j]);
|
||||
int k=Math.abs(product[i][j])-1;
|
||||
v.element[k]=v.element[k].add(product[i][j]<0?a.negate():a);
|
||||
}
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
public Generic divergence(Variable variable[]) {
|
||||
Generic a=JSCLInteger.valueOf(0);
|
||||
for(int i=0;i<n;i++) a=a.add(element[i].derivative(variable[i]));
|
||||
return a;
|
||||
}
|
||||
|
||||
public JSCLVector curl(Variable variable[]) {
|
||||
JSCLVector v=(JSCLVector)newinstance();
|
||||
v.element[0]=element[2].derivative(variable[1]).subtract(element[1].derivative(variable[2]));
|
||||
v.element[1]=element[0].derivative(variable[2]).subtract(element[2].derivative(variable[0]));
|
||||
v.element[2]=element[1].derivative(variable[0]).subtract(element[0].derivative(variable[1]));
|
||||
for(int i=3;i<n;i++) v.element[i]=element[i];
|
||||
return v;
|
||||
}
|
||||
|
||||
public Matrix jacobian(Variable variable[]) {
|
||||
Matrix m=new Matrix(new Generic[n][variable.length]);
|
||||
for(int i=0;i<n;i++) {
|
||||
for(int j=0;j<variable.length;j++) {
|
||||
m.element[i][j]=element[i].derivative(variable[j]);
|
||||
}
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
public Generic del(Variable variable[], int algebra[]) {
|
||||
return differential(new Clifford(algebra==null?new int[] {Clifford.log2e(n),0}:algebra).operator(),variable);
|
||||
}
|
||||
|
||||
JSCLVector differential(int product[][], Variable variable[]) {
|
||||
JSCLVector v=(JSCLVector)newinstance();
|
||||
for(int i=0;i<n;i++) v.element[i]=JSCLInteger.valueOf(0);
|
||||
int l=Clifford.log2e(n);
|
||||
for(int i=1;i<=l;i++) {
|
||||
for(int j=0;j<n;j++) {
|
||||
Generic a=element[j].derivative(variable[i-1]);
|
||||
int k=Math.abs(product[i][j])-1;
|
||||
v.element[k]=v.element[k].add(product[i][j]<0?a.negate():a);
|
||||
}
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
public Generic conjugate() {
|
||||
JSCLVector v=(JSCLVector)newinstance();
|
||||
for(int i=0;i<n;i++) {
|
||||
v.element[i]=new Conjugate(element[i]).evaluate();
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
public int compareTo(JSCLVector vector) {
|
||||
return ArrayComparator.comparator.compare(element,vector.element);
|
||||
}
|
||||
|
||||
public int compareTo(Generic generic) {
|
||||
if(generic instanceof JSCLVector) {
|
||||
return compareTo((JSCLVector)generic);
|
||||
} else {
|
||||
return compareTo(valueof(generic));
|
||||
}
|
||||
}
|
||||
|
||||
public static JSCLVector unity(int dimension) {
|
||||
JSCLVector v=new JSCLVector(new Generic[dimension]);
|
||||
for(int i=0;i<v.n;i++) {
|
||||
if(i==0) v.element[i]=JSCLInteger.valueOf(1);
|
||||
else v.element[i]=JSCLInteger.valueOf(0);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer buffer=new StringBuffer();
|
||||
buffer.append("{");
|
||||
for(int i=0;i<n;i++) {
|
||||
buffer.append(element[i]).append(i<n-1?", ":"");
|
||||
}
|
||||
buffer.append("}");
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
public String toJava() {
|
||||
StringBuffer buffer=new StringBuffer();
|
||||
buffer.append("new NumericVector(new Numeric[] {");
|
||||
for(int i=0;i<n;i++) {
|
||||
buffer.append(element[i].toJava()).append(i<n-1?", ":"");
|
||||
}
|
||||
buffer.append("})");
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
public void toMathML(MathML element, Object data) {
|
||||
int exponent=data instanceof Integer?((Integer)data).intValue():1;
|
||||
if(exponent==1) bodyToMathML(element);
|
||||
else {
|
||||
MathML e1=element.element("msup");
|
||||
bodyToMathML(e1);
|
||||
MathML e2=element.element("mn");
|
||||
e2.appendChild(element.text(String.valueOf(exponent)));
|
||||
e1.appendChild(e2);
|
||||
element.appendChild(e1);
|
||||
}
|
||||
}
|
||||
|
||||
protected void bodyToMathML(MathML e0) {
|
||||
MathML e1=e0.element("mfenced");
|
||||
MathML e2=e0.element("mtable");
|
||||
for(int i=0;i<n;i++) {
|
||||
MathML e3=e0.element("mtr");
|
||||
MathML e4=e0.element("mtd");
|
||||
element[i].toMathML(e4,null);
|
||||
e3.appendChild(e4);
|
||||
e2.appendChild(e3);
|
||||
}
|
||||
e1.appendChild(e2);
|
||||
e0.appendChild(e1);
|
||||
}
|
||||
|
||||
protected Generic newinstance() {
|
||||
return newinstance(new Generic[n]);
|
||||
}
|
||||
|
||||
protected Generic newinstance(Generic element[]) {
|
||||
return new JSCLVector(element);
|
||||
}
|
||||
}
|
||||
|
||||
class Clifford {
|
||||
int p,n;
|
||||
int operator[][];
|
||||
|
||||
Clifford(int algebra[]) {
|
||||
this(algebra[0],algebra[1]);
|
||||
}
|
||||
|
||||
Clifford(int p, int q) {
|
||||
this.p=p;
|
||||
n=p+q;
|
||||
int m=1<<n;
|
||||
operator=new int[m][m];
|
||||
for(int i=0;i<m;i++) {
|
||||
for(int j=0;j<m;j++) {
|
||||
int a=combination(i,n);
|
||||
int b=combination(j,n);
|
||||
int c=a^b;
|
||||
int l=location(c,n);
|
||||
boolean s=sign(a,b);
|
||||
int k=l+1;
|
||||
operator[i][j]=s?-k:k;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
boolean sign(int a, int b) {
|
||||
boolean s=false;
|
||||
for(int i=0;i<n;i++) {
|
||||
if((b&(1<<i))>0) {
|
||||
for(int j=i;j<n;j++) {
|
||||
if((a&(1<<j))>0 && (j>i || i>=p)) s=!s;
|
||||
}
|
||||
}
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
static int combination(int l, int n) {
|
||||
if(n<=2) return l;
|
||||
int b[]=new int[1];
|
||||
int l1=decimation(l,n,b);
|
||||
int c=combination(l1,n-1);
|
||||
return (c<<1)+b[0];
|
||||
}
|
||||
|
||||
static int location(int c, int n) {
|
||||
if(n<=2) return c;
|
||||
int c1=c>>1;
|
||||
int b=c&1;
|
||||
int l1=location(c1,n-1);
|
||||
return dilatation(l1,n,new int[] {b});
|
||||
}
|
||||
|
||||
static int decimation(int l, int n, int b[]) {
|
||||
int p=grade(l,n-1,1);
|
||||
int p1=(p+1)>>1;
|
||||
b[0]=p&1;
|
||||
return l-sum(p1,n-1);
|
||||
}
|
||||
|
||||
static int dilatation(int l, int n, int b[]) {
|
||||
int p1=grade(l,n-1);
|
||||
return l+sum(p1+b[0],n-1);
|
||||
}
|
||||
|
||||
static int grade(int l, int n) {
|
||||
return grade(l,n,0);
|
||||
}
|
||||
|
||||
static int grade(int l, int n, int d) {
|
||||
int s=0, p=0;
|
||||
while(true) {
|
||||
s+=binomial(n,p>>d);
|
||||
if(s<=l) p++;
|
||||
else break;
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
static int sum(int p, int n) {
|
||||
int q=0, s=0;
|
||||
while(q<p) s+=binomial(n,q++);
|
||||
return s;
|
||||
}
|
||||
|
||||
static int binomial(int n, int p) {
|
||||
int a=1, b=1;
|
||||
for(int i=n-p+1;i<=n;i++) a*=i;
|
||||
for(int i=2;i<=p;i++) b*=i;
|
||||
return a/b;
|
||||
}
|
||||
|
||||
static int log2e(int n) {
|
||||
int i;
|
||||
for(i=0;n>1;n>>=1) i++;
|
||||
return i;
|
||||
}
|
||||
|
||||
int[][] operator() {
|
||||
return operator;
|
||||
}
|
||||
}
|
368
src/misc/src/jscl/math/Literal.java
Normal file
368
src/misc/src/jscl/math/Literal.java
Normal file
@ -0,0 +1,368 @@
|
||||
package jscl.math;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
import jscl.math.function.Frac;
|
||||
import jscl.math.function.Pow;
|
||||
import jscl.math.polynomial.Monomial;
|
||||
import jscl.mathml.MathML;
|
||||
|
||||
public class Literal implements Comparable {
|
||||
Variable variable[];
|
||||
int power[];
|
||||
int degree;
|
||||
int size;
|
||||
|
||||
Literal() {}
|
||||
|
||||
Literal(int size) {
|
||||
init(size);
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public Variable variable(int n) {
|
||||
return variable[n];
|
||||
}
|
||||
|
||||
public int power(int n) {
|
||||
return power[n];
|
||||
}
|
||||
|
||||
void init(int size) {
|
||||
variable=new Variable[size];
|
||||
power=new int[size];
|
||||
this.size=size;
|
||||
}
|
||||
|
||||
void resize(int size) {
|
||||
if(size<variable.length) {
|
||||
Variable variable[]=new Variable[size];
|
||||
int power[]=new int[size];
|
||||
System.arraycopy(this.variable,0,variable,0,size);
|
||||
System.arraycopy(this.power,0,power,0,size);
|
||||
this.variable=variable;
|
||||
this.power=power;
|
||||
this.size=size;
|
||||
}
|
||||
}
|
||||
|
||||
public Literal multiply(Literal literal) {
|
||||
Literal l=newinstance(size+literal.size);
|
||||
int i=0;
|
||||
int i1=0;
|
||||
int i2=0;
|
||||
Variable v1=i1<size?variable[i1]:null;
|
||||
Variable v2=i2<literal.size?literal.variable[i2]:null;
|
||||
while(v1!=null || v2!=null) {
|
||||
int c=v1==null?1:(v2==null?-1:v1.compareTo(v2));
|
||||
if(c<0) {
|
||||
int s=power[i1];
|
||||
l.variable[i]=v1;
|
||||
l.power[i]=s;
|
||||
l.degree+=s;
|
||||
i++;
|
||||
i1++;
|
||||
v1=i1<size?variable[i1]:null;
|
||||
} else if(c>0) {
|
||||
int s=literal.power[i2];
|
||||
l.variable[i]=v2;
|
||||
l.power[i]=s;
|
||||
l.degree+=s;
|
||||
i++;
|
||||
i2++;
|
||||
v2=i2<literal.size?literal.variable[i2]:null;
|
||||
} else {
|
||||
int s=power[i1]+literal.power[i2];
|
||||
l.variable[i]=v1;
|
||||
l.power[i]=s;
|
||||
l.degree+=s;
|
||||
i++;
|
||||
i1++;
|
||||
i2++;
|
||||
v1=i1<size?variable[i1]:null;
|
||||
v2=i2<literal.size?literal.variable[i2]:null;
|
||||
}
|
||||
}
|
||||
l.resize(i);
|
||||
return l;
|
||||
}
|
||||
|
||||
public Literal divide(Literal literal) throws ArithmeticException {
|
||||
Literal l=newinstance(size+literal.size);
|
||||
int i=0;
|
||||
int i1=0;
|
||||
int i2=0;
|
||||
Variable v1=i1<size?variable[i1]:null;
|
||||
Variable v2=i2<literal.size?literal.variable[i2]:null;
|
||||
while(v1!=null || v2!=null) {
|
||||
int c=v1==null?1:(v2==null?-1:v1.compareTo(v2));
|
||||
if(c<0) {
|
||||
int s=power[i1];
|
||||
l.variable[i]=v1;
|
||||
l.power[i]=s;
|
||||
l.degree+=s;
|
||||
i++;
|
||||
i1++;
|
||||
v1=i1<size?variable[i1]:null;
|
||||
} else if(c>0) {
|
||||
throw new NotDivisibleException();
|
||||
} else {
|
||||
int s=power[i1]-literal.power[i2];
|
||||
if(s<0) throw new NotDivisibleException();
|
||||
else if(s==0);
|
||||
else {
|
||||
l.variable[i]=v1;
|
||||
l.power[i]=s;
|
||||
l.degree+=s;
|
||||
i++;
|
||||
}
|
||||
i1++;
|
||||
i2++;
|
||||
v1=i1<size?variable[i1]:null;
|
||||
v2=i2<literal.size?literal.variable[i2]:null;
|
||||
}
|
||||
}
|
||||
l.resize(i);
|
||||
return l;
|
||||
}
|
||||
|
||||
public Literal gcd(Literal literal) {
|
||||
Literal l=newinstance(Math.min(size,literal.size));
|
||||
int i=0;
|
||||
int i1=0;
|
||||
int i2=0;
|
||||
Variable v1=i1<size?variable[i1]:null;
|
||||
Variable v2=i2<literal.size?literal.variable[i2]:null;
|
||||
while(v1!=null || v2!=null) {
|
||||
int c=v1==null?1:(v2==null?-1:v1.compareTo(v2));
|
||||
if(c<0) {
|
||||
i1++;
|
||||
v1=i1<size?variable[i1]:null;
|
||||
} else if(c>0) {
|
||||
i2++;
|
||||
v2=i2<literal.size?literal.variable[i2]:null;
|
||||
} else {
|
||||
int s=Math.min(power[i1],literal.power[i2]);
|
||||
l.variable[i]=v1;
|
||||
l.power[i]=s;
|
||||
l.degree+=s;
|
||||
i++;
|
||||
i1++;
|
||||
i2++;
|
||||
v1=i1<size?variable[i1]:null;
|
||||
v2=i2<literal.size?literal.variable[i2]:null;
|
||||
}
|
||||
}
|
||||
l.resize(i);
|
||||
return l;
|
||||
}
|
||||
|
||||
public Literal scm(Literal literal) {
|
||||
Literal l=newinstance(size+literal.size);
|
||||
int i=0;
|
||||
int i1=0;
|
||||
int i2=0;
|
||||
Variable v1=i1<size?variable[i1]:null;
|
||||
Variable v2=i2<literal.size?literal.variable[i2]:null;
|
||||
while(v1!=null || v2!=null) {
|
||||
int c=v1==null?1:(v2==null?-1:v1.compareTo(v2));
|
||||
if(c<0) {
|
||||
int s=power[i1];
|
||||
l.variable[i]=v1;
|
||||
l.power[i]=s;
|
||||
l.degree+=s;
|
||||
i++;
|
||||
i1++;
|
||||
v1=i1<size?variable[i1]:null;
|
||||
} else if(c>0) {
|
||||
int s=literal.power[i2];
|
||||
l.variable[i]=v2;
|
||||
l.power[i]=s;
|
||||
l.degree+=s;
|
||||
i++;
|
||||
i2++;
|
||||
v2=i2<literal.size?literal.variable[i2]:null;
|
||||
} else {
|
||||
int s=Math.max(power[i1],literal.power[i2]);
|
||||
l.variable[i]=v1;
|
||||
l.power[i]=s;
|
||||
l.degree+=s;
|
||||
i++;
|
||||
i1++;
|
||||
i2++;
|
||||
v1=i1<size?variable[i1]:null;
|
||||
v2=i2<literal.size?literal.variable[i2]:null;
|
||||
}
|
||||
}
|
||||
l.resize(i);
|
||||
return l;
|
||||
}
|
||||
|
||||
public Generic[] productValue() throws NotProductException {
|
||||
Generic a[]=new Generic[size];
|
||||
for(int i=0;i<a.length;i++) a[i]=variable[i].expressionValue().pow(power[i]);
|
||||
return a;
|
||||
}
|
||||
|
||||
public Power powerValue() throws NotPowerException {
|
||||
if(size==0) return new Power(JSCLInteger.valueOf(1),1);
|
||||
else if(size==1) {
|
||||
Variable v=variable[0];
|
||||
int c=power[0];
|
||||
return new Power(v.expressionValue(),c);
|
||||
} else throw new NotPowerException();
|
||||
}
|
||||
|
||||
public Variable variableValue() throws NotVariableException {
|
||||
if(size==0) throw new NotVariableException();
|
||||
else if(size==1) {
|
||||
Variable v=variable[0];
|
||||
int c=power[0];
|
||||
if(c==1) return v;
|
||||
else throw new NotVariableException();
|
||||
} else throw new NotVariableException();
|
||||
}
|
||||
|
||||
public Variable[] variables() {
|
||||
Variable va[]=new Variable[size];
|
||||
System.arraycopy(variable,0,va,0,size);
|
||||
return va;
|
||||
}
|
||||
|
||||
public int degree() {
|
||||
return degree;
|
||||
}
|
||||
|
||||
public int compareTo(Literal literal) {
|
||||
int i1=size;
|
||||
int i2=literal.size;
|
||||
Variable v1=i1==0?null:variable[--i1];
|
||||
Variable v2=i2==0?null:literal.variable[--i2];
|
||||
while(v1!=null || v2!=null) {
|
||||
int c=v1==null?-1:(v2==null?1:v1.compareTo(v2));
|
||||
if(c<0) return -1;
|
||||
else if(c>0) return 1;
|
||||
else {
|
||||
int c1=power[i1];
|
||||
int c2=literal.power[i2];
|
||||
if(c1<c2) return -1;
|
||||
else if(c1>c2) return 1;
|
||||
v1=i1==0?null:variable[--i1];
|
||||
v2=i2==0?null:literal.variable[--i2];
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public int compareTo(Object o) {
|
||||
return compareTo((Literal)o);
|
||||
}
|
||||
|
||||
public static Literal valueOf() {
|
||||
return new Literal(0);
|
||||
}
|
||||
|
||||
public static Literal valueOf(Variable variable) {
|
||||
return valueOf(variable,1);
|
||||
}
|
||||
|
||||
public static Literal valueOf(Variable variable, int power) {
|
||||
Literal l=new Literal();
|
||||
l.init(variable,power);
|
||||
return l;
|
||||
}
|
||||
|
||||
void init(Variable var, int pow) {
|
||||
if(pow!=0) {
|
||||
init(1);
|
||||
variable[0]=var;
|
||||
power[0]=pow;
|
||||
degree=pow;
|
||||
} else init(0);
|
||||
}
|
||||
|
||||
public static Literal valueOf(Monomial monomial) {
|
||||
Literal l=new Literal();
|
||||
l.init(monomial);
|
||||
return l;
|
||||
}
|
||||
|
||||
void init(Monomial monomial) {
|
||||
Map map=new TreeMap();
|
||||
Variable unk[]=monomial.unknown();
|
||||
for(int i=0;i<unk.length;i++) {
|
||||
int c=monomial.element(i);
|
||||
if(c>0) map.put(unk[i],new Integer(c));
|
||||
}
|
||||
init(map.size());
|
||||
Iterator it=map.entrySet().iterator();
|
||||
for(int i=0;it.hasNext();i++) {
|
||||
Map.Entry e=(Map.Entry)it.next();
|
||||
Variable v=(Variable)e.getKey();
|
||||
int c=((Integer)e.getValue()).intValue();
|
||||
variable[i]=v;
|
||||
power[i]=c;
|
||||
degree+=c;
|
||||
}
|
||||
}
|
||||
|
||||
Map content() {
|
||||
Map map=new TreeMap();
|
||||
for(int i=0;i<size;i++) map.put(variable[i],new Integer(power[i]));
|
||||
return map;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer buffer=new StringBuffer();
|
||||
if(degree==0) buffer.append("1");
|
||||
for(int i=0;i<size;i++) {
|
||||
if(i>0) buffer.append("*");
|
||||
Variable v=variable[i];
|
||||
int c=power[i];
|
||||
if(c==1) buffer.append(v);
|
||||
else {
|
||||
if(v instanceof Frac || v instanceof Pow) {
|
||||
buffer.append("(").append(v).append(")");
|
||||
} else buffer.append(v);
|
||||
buffer.append("^").append(c);
|
||||
}
|
||||
}
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
public String toJava() {
|
||||
StringBuffer buffer=new StringBuffer();
|
||||
if(degree==0) buffer.append("JSCLDouble.valueOf(1)");
|
||||
for(int i=0;i<size;i++) {
|
||||
if(i>0) buffer.append(".multiply(");
|
||||
Variable v=variable[i];
|
||||
int c=power[i];
|
||||
buffer.append(v.toJava());
|
||||
if(c==1);
|
||||
else buffer.append(".pow(").append(c).append(")");
|
||||
if(i>0) buffer.append(")");
|
||||
}
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
public void toMathML(MathML element, Object data) {
|
||||
if(degree==0) {
|
||||
MathML e1=element.element("mn");
|
||||
e1.appendChild(element.text("1"));
|
||||
element.appendChild(e1);
|
||||
}
|
||||
for(int i=0;i<size;i++) {
|
||||
Variable v=variable[i];
|
||||
int c=power[i];
|
||||
v.toMathML(element,new Integer(c));
|
||||
}
|
||||
}
|
||||
|
||||
protected Literal newinstance(int n) {
|
||||
return new Literal(n);
|
||||
}
|
||||
}
|
497
src/misc/src/jscl/math/Matrix.java
Normal file
497
src/misc/src/jscl/math/Matrix.java
Normal file
@ -0,0 +1,497 @@
|
||||
package jscl.math;
|
||||
|
||||
import jscl.math.function.Conjugate;
|
||||
import jscl.math.function.Frac;
|
||||
import jscl.math.function.trigonometric.Cos;
|
||||
import jscl.math.function.trigonometric.Sin;
|
||||
import jscl.mathml.MathML;
|
||||
import jscl.util.ArrayComparator;
|
||||
|
||||
public class Matrix extends Generic {
|
||||
protected final Generic element[][];
|
||||
protected final int n,p;
|
||||
|
||||
public Matrix(Generic element[][]) {
|
||||
this.element=element;
|
||||
n=element.length;
|
||||
p=element.length>0?element[0].length:0;
|
||||
}
|
||||
|
||||
public Generic[][] elements() {
|
||||
return element;
|
||||
}
|
||||
|
||||
public Matrix add(Matrix matrix) {
|
||||
Matrix m=(Matrix)newinstance();
|
||||
for(int i=0;i<n;i++) {
|
||||
for(int j=0;j<p;j++) {
|
||||
m.element[i][j]=element[i][j].add(matrix.element[i][j]);
|
||||
}
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
public Generic add(Generic generic) {
|
||||
if(generic instanceof Matrix) {
|
||||
return add((Matrix)generic);
|
||||
} else {
|
||||
return add(valueof(generic));
|
||||
}
|
||||
}
|
||||
|
||||
public Matrix subtract(Matrix matrix) {
|
||||
Matrix m=(Matrix)newinstance();
|
||||
for(int i=0;i<n;i++) {
|
||||
for(int j=0;j<p;j++) {
|
||||
m.element[i][j]=element[i][j].subtract(matrix.element[i][j]);
|
||||
}
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
public Generic subtract(Generic generic) {
|
||||
if(generic instanceof Matrix) {
|
||||
return subtract((Matrix)generic);
|
||||
} else {
|
||||
return subtract(valueof(generic));
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean product(Generic a, Generic b) {
|
||||
return (a instanceof Matrix && b instanceof Matrix) || (a instanceof Matrix && b instanceof JSCLVector) || (a instanceof JSCLVector && b instanceof Matrix);
|
||||
}
|
||||
|
||||
public Matrix multiply(Matrix matrix) {
|
||||
if(p!=matrix.n) throw new ArithmeticException();
|
||||
Matrix m=(Matrix)newinstance(new Generic[n][matrix.p]);
|
||||
for(int i=0;i<n;i++) {
|
||||
for(int j=0;j<matrix.p;j++) {
|
||||
m.element[i][j]=JSCLInteger.valueOf(0);
|
||||
for(int k=0;k<p;k++) {
|
||||
m.element[i][j]=m.element[i][j].add(element[i][k].multiply(matrix.element[k][j]));
|
||||
}
|
||||
}
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
public Generic multiply(Generic generic) {
|
||||
if(generic instanceof Matrix) {
|
||||
return multiply((Matrix)generic);
|
||||
} else if(generic instanceof JSCLVector) {
|
||||
JSCLVector v=(JSCLVector)((JSCLVector)generic).newinstance(new Generic[n]);
|
||||
JSCLVector v2=(JSCLVector)generic;
|
||||
if(p!=v2.n) throw new ArithmeticException();
|
||||
for(int i=0;i<n;i++) {
|
||||
v.element[i]=JSCLInteger.valueOf(0);
|
||||
for(int k=0;k<p;k++) {
|
||||
v.element[i]=v.element[i].add(element[i][k].multiply(v2.element[k]));
|
||||
}
|
||||
}
|
||||
return v;
|
||||
} else {
|
||||
Matrix m=(Matrix)newinstance();
|
||||
for(int i=0;i<n;i++) {
|
||||
for(int j=0;j<p;j++) {
|
||||
m.element[i][j]=element[i][j].multiply(generic);
|
||||
}
|
||||
}
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
public Generic divide(Generic generic) throws ArithmeticException {
|
||||
if(generic instanceof Matrix) {
|
||||
return multiply(((Matrix)generic).inverse());
|
||||
} else if(generic instanceof JSCLVector) {
|
||||
throw new ArithmeticException();
|
||||
} else {
|
||||
Matrix m=(Matrix)newinstance();
|
||||
for(int i=0;i<n;i++) {
|
||||
for(int j=0;j<p;j++) {
|
||||
try {
|
||||
m.element[i][j]=element[i][j].divide(generic);
|
||||
} catch (NotDivisibleException e) {
|
||||
m.element[i][j]=new Frac(element[i][j],generic).evaluate();
|
||||
}
|
||||
}
|
||||
}
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
public Generic gcd(Generic generic) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public Generic gcd() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public Generic negate() {
|
||||
Matrix m=(Matrix)newinstance();
|
||||
for(int i=0;i<n;i++) {
|
||||
for(int j=0;j<p;j++) {
|
||||
m.element[i][j]=element[i][j].negate();
|
||||
}
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
public int signum() {
|
||||
for(int i=0;i<n;i++) {
|
||||
for(int j=0;j<p;j++) {
|
||||
int c=element[i][j].signum();
|
||||
if(c<0) return -1;
|
||||
else if(c>0) return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public int degree() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public Generic antiderivative(Variable variable) throws NotIntegrableException {
|
||||
Matrix m=(Matrix)newinstance();
|
||||
for(int i=0;i<n;i++) {
|
||||
for(int j=0;j<p;j++) {
|
||||
m.element[i][j]=element[i][j].antiderivative(variable);
|
||||
}
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
public Generic derivative(Variable variable) {
|
||||
Matrix m=(Matrix)newinstance();
|
||||
for(int i=0;i<n;i++) {
|
||||
for(int j=0;j<p;j++) {
|
||||
m.element[i][j]=element[i][j].derivative(variable);
|
||||
}
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
public Generic substitute(Variable variable, Generic generic) {
|
||||
Matrix m=(Matrix)newinstance();
|
||||
for(int i=0;i<n;i++) {
|
||||
for(int j=0;j<p;j++) {
|
||||
m.element[i][j]=element[i][j].substitute(variable,generic);
|
||||
}
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
public Generic expand() {
|
||||
Matrix m=(Matrix)newinstance();
|
||||
for(int i=0;i<n;i++) {
|
||||
for(int j=0;j<p;j++) {
|
||||
m.element[i][j]=element[i][j].expand();
|
||||
}
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
public Generic factorize() {
|
||||
Matrix m=(Matrix)newinstance();
|
||||
for(int i=0;i<n;i++) {
|
||||
for(int j=0;j<p;j++) {
|
||||
m.element[i][j]=element[i][j].factorize();
|
||||
}
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
public Generic elementary() {
|
||||
Matrix m=(Matrix)newinstance();
|
||||
for(int i=0;i<n;i++) {
|
||||
for(int j=0;j<p;j++) {
|
||||
m.element[i][j]=element[i][j].elementary();
|
||||
}
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
public Generic simplify() {
|
||||
Matrix m=(Matrix)newinstance();
|
||||
for(int i=0;i<n;i++) {
|
||||
for(int j=0;j<p;j++) {
|
||||
m.element[i][j]=element[i][j].simplify();
|
||||
}
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
public Generic numeric() {
|
||||
return new NumericWrapper(this);
|
||||
}
|
||||
|
||||
public Generic valueof(Generic generic) {
|
||||
if(generic instanceof Matrix || generic instanceof JSCLVector) {
|
||||
throw new ArithmeticException();
|
||||
} else {
|
||||
Matrix m=(Matrix)identity(n,p).multiply(generic);
|
||||
return newinstance(m.element);
|
||||
}
|
||||
}
|
||||
|
||||
public Generic[] sumValue() {
|
||||
return new Generic[] {this};
|
||||
}
|
||||
|
||||
public Generic[] productValue() throws NotProductException {
|
||||
return new Generic[] {this};
|
||||
}
|
||||
|
||||
public Power powerValue() throws NotPowerException {
|
||||
return new Power(this,1);
|
||||
}
|
||||
|
||||
public Expression expressionValue() throws NotExpressionException {
|
||||
throw new NotExpressionException();
|
||||
}
|
||||
|
||||
public JSCLInteger integerValue() throws NotIntegerException {
|
||||
throw new NotIntegerException();
|
||||
}
|
||||
|
||||
public Variable variableValue() throws NotVariableException {
|
||||
throw new NotVariableException();
|
||||
}
|
||||
|
||||
public Variable[] variables() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean isPolynomial(Variable variable) {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isConstant(Variable variable) {
|
||||
return false;
|
||||
}
|
||||
|
||||
public Generic[] vectors() {
|
||||
JSCLVector v[]=new JSCLVector[n];
|
||||
for(int i=0;i<n;i++) {
|
||||
v[i]=new JSCLVector(new Generic[p]);
|
||||
for(int j=0;j<p;j++) {
|
||||
v[i].element[j]=element[i][j];
|
||||
}
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
public Generic tensorProduct(Matrix matrix) {
|
||||
Matrix m=(Matrix)newinstance(new Generic[n*matrix.n][p*matrix.p]);
|
||||
for(int i=0;i<n;i++) {
|
||||
for(int j=0;j<p;j++) {
|
||||
for(int k=0;k<matrix.n;k++) {
|
||||
for(int l=0;l<matrix.p;l++) {
|
||||
m.element[i*matrix.n+k][j*matrix.p+l]=element[i][j].multiply(matrix.element[k][l]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
public Generic transpose() {
|
||||
Matrix m=(Matrix)newinstance(new Generic[p][n]);
|
||||
for(int i=0;i<n;i++) {
|
||||
for(int j=0;j<p;j++) {
|
||||
m.element[j][i]=element[i][j];
|
||||
}
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
public Generic trace() {
|
||||
Generic s=JSCLInteger.valueOf(0);
|
||||
for(int i=0;i<n;i++) {
|
||||
s=s.add(element[i][i]);
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
public Generic inverse() {
|
||||
Matrix m=(Matrix)newinstance();
|
||||
for(int i=0;i<n;i++) {
|
||||
for(int j=0;j<n;j++) {
|
||||
m.element[i][j]=inverseElement(i,j);
|
||||
}
|
||||
}
|
||||
return m.transpose().divide(determinant());
|
||||
}
|
||||
|
||||
Generic inverseElement(int k, int l) {
|
||||
Matrix m=(Matrix)newinstance();
|
||||
for(int i=0;i<n;i++) {
|
||||
for(int j=0;j<n;j++) {
|
||||
m.element[i][j]=i==k?JSCLInteger.valueOf(j==l?1:0):element[i][j];
|
||||
}
|
||||
}
|
||||
return m.determinant();
|
||||
}
|
||||
|
||||
public Generic determinant() {
|
||||
if(n>1) {
|
||||
Generic a=JSCLInteger.valueOf(0);
|
||||
for(int i=0;i<n;i++) {
|
||||
if(element[i][0].signum()==0);
|
||||
else {
|
||||
Matrix m=(Matrix)newinstance(new Generic[n-1][n-1]);
|
||||
for(int j=0;j<n-1;j++) {
|
||||
for(int k=0;k<n-1;k++) m.element[j][k]=element[j<i?j:j+1][k+1];
|
||||
}
|
||||
if(i%2==0) a=a.add(element[i][0].multiply(m.determinant()));
|
||||
else a=a.subtract(element[i][0].multiply(m.determinant()));
|
||||
}
|
||||
}
|
||||
return a;
|
||||
} else if(n>0) return element[0][0];
|
||||
else return JSCLInteger.valueOf(0);
|
||||
}
|
||||
|
||||
public Generic conjugate() {
|
||||
Matrix m=(Matrix)newinstance();
|
||||
for(int i=0;i<n;i++) {
|
||||
for(int j=0;j<p;j++) {
|
||||
m.element[i][j]=new Conjugate(element[i][j]).evaluate();
|
||||
}
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
public int compareTo(Matrix matrix) {
|
||||
return ArrayComparator.comparator.compare(vectors(),matrix.vectors());
|
||||
}
|
||||
|
||||
public int compareTo(Generic generic) {
|
||||
if(generic instanceof Matrix) {
|
||||
return compareTo((Matrix)generic);
|
||||
} else {
|
||||
return compareTo(valueof(generic));
|
||||
}
|
||||
}
|
||||
|
||||
public static Matrix identity(int dimension) {
|
||||
return identity(dimension,dimension);
|
||||
}
|
||||
|
||||
public static Matrix identity(int n, int p) {
|
||||
Matrix m=new Matrix(new Generic[n][p]);
|
||||
for(int i=0;i<n;i++) {
|
||||
for(int j=0;j<p;j++) {
|
||||
if(i==j) {
|
||||
m.element[i][j]=JSCLInteger.valueOf(1);
|
||||
} else {
|
||||
m.element[i][j]=JSCLInteger.valueOf(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
public static Matrix frame(JSCLVector vector[]) {
|
||||
Matrix m=new Matrix(new Generic[vector.length>0?vector[0].n:0][vector.length]);
|
||||
for(int i=0;i<m.n;i++) {
|
||||
for(int j=0;j<m.p;j++) {
|
||||
m.element[i][j]=vector[j].element[i];
|
||||
}
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
public static Matrix rotation(int dimension, int plane, Generic angle) {
|
||||
return rotation(dimension,plane,2,angle);
|
||||
}
|
||||
|
||||
public static Matrix rotation(int dimension, int axis1, int axis2, Generic angle) {
|
||||
Matrix m=new Matrix(new Generic[dimension][dimension]);
|
||||
for(int i=0;i<m.n;i++) {
|
||||
for(int j=0;j<m.p;j++) {
|
||||
if(i==axis1 && j==axis1) {
|
||||
m.element[i][j]=new Cos(angle).evaluate();
|
||||
} else if(i==axis1 && j==axis2) {
|
||||
m.element[i][j]=new Sin(angle).evaluate().negate();
|
||||
} else if(i==axis2 && j==axis1) {
|
||||
m.element[i][j]=new Sin(angle).evaluate();
|
||||
} else if(i==axis2 && j==axis2) {
|
||||
m.element[i][j]=new Cos(angle).evaluate();
|
||||
} else if(i==j) {
|
||||
m.element[i][j]=JSCLInteger.valueOf(1);
|
||||
} else {
|
||||
m.element[i][j]=JSCLInteger.valueOf(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer buffer=new StringBuffer();
|
||||
buffer.append("{");
|
||||
for(int i=0;i<n;i++) {
|
||||
buffer.append("{");
|
||||
for(int j=0;j<p;j++) {
|
||||
buffer.append(element[i][j]).append(j<p-1?", ":"");
|
||||
}
|
||||
buffer.append("}").append(i<n-1?",\n":"");
|
||||
}
|
||||
buffer.append("}");
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
public String toJava() {
|
||||
StringBuffer buffer=new StringBuffer();
|
||||
buffer.append("new NumericMatrix(new Numeric[][] {");
|
||||
for(int i=0;i<n;i++) {
|
||||
buffer.append("{");
|
||||
for(int j=0;j<p;j++) {
|
||||
buffer.append(element[i][j].toJava()).append(j<p-1?", ":"");
|
||||
}
|
||||
buffer.append("}").append(i<n-1?", ":"");
|
||||
}
|
||||
buffer.append("})");
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
public void toMathML(MathML element, Object data) {
|
||||
int exponent=data instanceof Integer?((Integer)data).intValue():1;
|
||||
if(exponent==1) bodyToMathML(element);
|
||||
else {
|
||||
MathML e1=element.element("msup");
|
||||
bodyToMathML(e1);
|
||||
MathML e2=element.element("mn");
|
||||
e2.appendChild(element.text(String.valueOf(exponent)));
|
||||
e1.appendChild(e2);
|
||||
element.appendChild(e1);
|
||||
}
|
||||
}
|
||||
|
||||
protected void bodyToMathML(MathML e0) {
|
||||
MathML e1=e0.element("mfenced");
|
||||
MathML e2=e0.element("mtable");
|
||||
for(int i=0;i<n;i++) {
|
||||
MathML e3=e0.element("mtr");
|
||||
for(int j=0;j<p;j++) {
|
||||
MathML e4=e0.element("mtd");
|
||||
element[i][j].toMathML(e4,null);
|
||||
e3.appendChild(e4);
|
||||
}
|
||||
e2.appendChild(e3);
|
||||
}
|
||||
e1.appendChild(e2);
|
||||
e0.appendChild(e1);
|
||||
}
|
||||
|
||||
protected Generic newinstance() {
|
||||
return newinstance(new Generic[n][p]);
|
||||
}
|
||||
|
||||
protected Generic newinstance(Generic element[][]) {
|
||||
return new Matrix(element);
|
||||
}
|
||||
}
|
11
src/misc/src/jscl/math/MatrixVariable.java
Normal file
11
src/misc/src/jscl/math/MatrixVariable.java
Normal file
@ -0,0 +1,11 @@
|
||||
package jscl.math;
|
||||
|
||||
public class MatrixVariable extends GenericVariable {
|
||||
public MatrixVariable(Generic generic) {
|
||||
super(generic);
|
||||
}
|
||||
|
||||
protected Variable newinstance() {
|
||||
return new MatrixVariable(null);
|
||||
}
|
||||
}
|
189
src/misc/src/jscl/math/ModularInteger.java
Normal file
189
src/misc/src/jscl/math/ModularInteger.java
Normal file
@ -0,0 +1,189 @@
|
||||
package jscl.math;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import jscl.mathml.MathML;
|
||||
|
||||
public class ModularInteger extends Generic implements Field {
|
||||
public static final ModularInteger booleanFactory=new ModularInteger(0,2);
|
||||
final int modulo;
|
||||
final int content;
|
||||
|
||||
public ModularInteger(long content, int modulo) {
|
||||
this.modulo=modulo;
|
||||
this.content=(int)(content%modulo);
|
||||
}
|
||||
|
||||
public int content() {
|
||||
return content;
|
||||
}
|
||||
|
||||
public int modulo() {
|
||||
return modulo;
|
||||
}
|
||||
|
||||
public ModularInteger add(ModularInteger integer) {
|
||||
return newinstance((long)content+integer.content);
|
||||
}
|
||||
|
||||
public Generic add(Generic generic) {
|
||||
return add((ModularInteger)generic);
|
||||
}
|
||||
|
||||
public ModularInteger subtract(ModularInteger integer) {
|
||||
return newinstance((long)content+(modulo-integer.content));
|
||||
}
|
||||
|
||||
public Generic subtract(Generic generic) {
|
||||
return subtract((ModularInteger)generic);
|
||||
}
|
||||
|
||||
public ModularInteger multiply(ModularInteger integer) {
|
||||
return newinstance((long)content*integer.content);
|
||||
}
|
||||
|
||||
public Generic multiply(Generic generic) {
|
||||
return multiply((ModularInteger)generic);
|
||||
}
|
||||
|
||||
public Generic divide(Generic generic) throws ArithmeticException {
|
||||
return multiply(generic.inverse());
|
||||
}
|
||||
|
||||
public Generic inverse() {
|
||||
return newinstance(BigInteger.valueOf(content).modInverse(BigInteger.valueOf(modulo)).intValue());
|
||||
}
|
||||
|
||||
public Generic gcd(Generic generic) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public Generic gcd() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public Generic pow(int exponent) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public Generic negate() {
|
||||
return newinstance(modulo-content);
|
||||
}
|
||||
|
||||
public int signum() {
|
||||
return content>0?1:0;
|
||||
}
|
||||
|
||||
public int degree() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public Generic antiderivative(Variable variable) throws NotIntegrableException {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public Generic derivative(Variable variable) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public Generic substitute(Variable variable, Generic generic) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public Generic expand() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public Generic factorize() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public Generic elementary() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public Generic simplify() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public Generic numeric() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public Generic valueof(Generic generic) {
|
||||
if(generic instanceof ModularInteger) {
|
||||
return newinstance(((ModularInteger)generic).content);
|
||||
} else {
|
||||
return newinstance(((JSCLInteger)generic).content().mod(BigInteger.valueOf(modulo)).intValue());
|
||||
}
|
||||
}
|
||||
|
||||
public Generic[] sumValue() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public Generic[] productValue() throws NotProductException {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public Power powerValue() throws NotPowerException {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public Expression expressionValue() throws NotExpressionException {
|
||||
return Expression.valueOf(integerValue());
|
||||
}
|
||||
|
||||
public JSCLInteger integerValue() throws NotIntegerException {
|
||||
return JSCLInteger.valueOf(content);
|
||||
}
|
||||
|
||||
public Variable variableValue() throws NotVariableException {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public Variable[] variables() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public boolean isPolynomial(Variable variable) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public boolean isConstant(Variable variable) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public int compareTo(ModularInteger integer) {
|
||||
return content<integer.content?-1:content>integer.content?1:0;
|
||||
}
|
||||
|
||||
public int compareTo(Generic generic) {
|
||||
if(generic instanceof ModularInteger) {
|
||||
return compareTo((ModularInteger)generic);
|
||||
} else if(generic instanceof JSCLInteger) {
|
||||
return compareTo(valueof(generic));
|
||||
} else {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
|
||||
public static ModularInteger factory(int modulo) {
|
||||
return new ModularInteger(0,modulo);
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return ""+content;
|
||||
}
|
||||
|
||||
public String toJava() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public void toMathML(MathML element, Object data) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
protected ModularInteger newinstance(long content) {
|
||||
return new ModularInteger(content,modulo);
|
||||
}
|
||||
}
|
9
src/misc/src/jscl/math/NotDivisibleException.java
Normal file
9
src/misc/src/jscl/math/NotDivisibleException.java
Normal file
@ -0,0 +1,9 @@
|
||||
package jscl.math;
|
||||
|
||||
public class NotDivisibleException extends ArithmeticException {
|
||||
public NotDivisibleException() {}
|
||||
|
||||
public NotDivisibleException(String s) {
|
||||
super(s);
|
||||
}
|
||||
}
|
9
src/misc/src/jscl/math/NotExpressionException.java
Normal file
9
src/misc/src/jscl/math/NotExpressionException.java
Normal file
@ -0,0 +1,9 @@
|
||||
package jscl.math;
|
||||
|
||||
public class NotExpressionException extends ArithmeticException {
|
||||
public NotExpressionException() {}
|
||||
|
||||
public NotExpressionException(String s) {
|
||||
super(s);
|
||||
}
|
||||
}
|
9
src/misc/src/jscl/math/NotIntegerException.java
Normal file
9
src/misc/src/jscl/math/NotIntegerException.java
Normal file
@ -0,0 +1,9 @@
|
||||
package jscl.math;
|
||||
|
||||
public class NotIntegerException extends ArithmeticException {
|
||||
public NotIntegerException() {}
|
||||
|
||||
public NotIntegerException(String s) {
|
||||
super(s);
|
||||
}
|
||||
}
|
9
src/misc/src/jscl/math/NotIntegrableException.java
Normal file
9
src/misc/src/jscl/math/NotIntegrableException.java
Normal file
@ -0,0 +1,9 @@
|
||||
package jscl.math;
|
||||
|
||||
public class NotIntegrableException extends ArithmeticException {
|
||||
public NotIntegrableException() {}
|
||||
|
||||
public NotIntegrableException(String s) {
|
||||
super(s);
|
||||
}
|
||||
}
|
9
src/misc/src/jscl/math/NotPowerException.java
Normal file
9
src/misc/src/jscl/math/NotPowerException.java
Normal file
@ -0,0 +1,9 @@
|
||||
package jscl.math;
|
||||
|
||||
public class NotPowerException extends ArithmeticException {
|
||||
public NotPowerException() {}
|
||||
|
||||
public NotPowerException(String s) {
|
||||
super(s);
|
||||
}
|
||||
}
|
9
src/misc/src/jscl/math/NotProductException.java
Normal file
9
src/misc/src/jscl/math/NotProductException.java
Normal file
@ -0,0 +1,9 @@
|
||||
package jscl.math;
|
||||
|
||||
public class NotProductException extends ArithmeticException {
|
||||
public NotProductException() {}
|
||||
|
||||
public NotProductException(String s) {
|
||||
super(s);
|
||||
}
|
||||
}
|
9
src/misc/src/jscl/math/NotVariableException.java
Normal file
9
src/misc/src/jscl/math/NotVariableException.java
Normal file
@ -0,0 +1,9 @@
|
||||
package jscl.math;
|
||||
|
||||
public class NotVariableException extends ArithmeticException {
|
||||
public NotVariableException() {}
|
||||
|
||||
public NotVariableException(String s) {
|
||||
super(s);
|
||||
}
|
||||
}
|
344
src/misc/src/jscl/math/NumericWrapper.java
Normal file
344
src/misc/src/jscl/math/NumericWrapper.java
Normal file
@ -0,0 +1,344 @@
|
||||
package jscl.math;
|
||||
|
||||
import jscl.math.function.Constant;
|
||||
import jscl.math.numeric.JSCLDouble;
|
||||
import jscl.math.numeric.Numeric;
|
||||
import jscl.math.numeric.NumericMatrix;
|
||||
import jscl.math.numeric.NumericVector;
|
||||
import jscl.mathml.MathML;
|
||||
|
||||
public final class NumericWrapper extends Generic {
|
||||
final Numeric content;
|
||||
|
||||
public NumericWrapper(JSCLInteger integer) {
|
||||
content=JSCLDouble.valueOf(integer.content().doubleValue());
|
||||
}
|
||||
|
||||
public NumericWrapper(Rational rational) {
|
||||
content=JSCLDouble.valueOf(rational.numerator().doubleValue()/rational.denominator().doubleValue());
|
||||
}
|
||||
|
||||
public NumericWrapper(JSCLVector vector) {
|
||||
Numeric v[]=new Numeric[vector.n];
|
||||
for(int i=0;i<vector.n;i++) v[i]=((NumericWrapper)vector.element[i].numeric()).content();
|
||||
content=new NumericVector(v);
|
||||
}
|
||||
|
||||
public NumericWrapper(Matrix matrix) {
|
||||
Numeric m[][]=new Numeric[matrix.n][matrix.p];
|
||||
for(int i=0;i<matrix.n;i++) {
|
||||
for(int j=0;j<matrix.p;j++) {
|
||||
m[i][j]=((NumericWrapper)matrix.element[i][j].numeric()).content();
|
||||
}
|
||||
}
|
||||
content=new NumericMatrix(m);
|
||||
}
|
||||
|
||||
public NumericWrapper(Constant constant) {
|
||||
if(constant.compareTo(new Constant("pi"))==0) content=JSCLDouble.valueOf(Math.PI);
|
||||
else if(constant.compareTo(new Constant("infin"))==0) content=JSCLDouble.valueOf(Double.POSITIVE_INFINITY);
|
||||
else throw new ArithmeticException();
|
||||
}
|
||||
|
||||
public NumericWrapper(Numeric numeric) {
|
||||
content=numeric;
|
||||
}
|
||||
|
||||
public Numeric content() {
|
||||
return content;
|
||||
}
|
||||
|
||||
public NumericWrapper add(NumericWrapper wrapper) {
|
||||
return new NumericWrapper(content.add(wrapper.content));
|
||||
}
|
||||
|
||||
public Generic add(Generic generic) {
|
||||
if(generic instanceof NumericWrapper) {
|
||||
return add((NumericWrapper)generic);
|
||||
} else {
|
||||
return add(valueof(generic));
|
||||
}
|
||||
}
|
||||
|
||||
public NumericWrapper subtract(NumericWrapper wrapper) {
|
||||
return new NumericWrapper(content.subtract(wrapper.content));
|
||||
}
|
||||
|
||||
public Generic subtract(Generic generic) {
|
||||
if(generic instanceof NumericWrapper) {
|
||||
return subtract((NumericWrapper)generic);
|
||||
} else {
|
||||
return subtract(valueof(generic));
|
||||
}
|
||||
}
|
||||
|
||||
public NumericWrapper multiply(NumericWrapper wrapper) {
|
||||
return new NumericWrapper(content.multiply(wrapper.content));
|
||||
}
|
||||
|
||||
public Generic multiply(Generic generic) {
|
||||
if(generic instanceof NumericWrapper) {
|
||||
return multiply((NumericWrapper)generic);
|
||||
} else {
|
||||
return multiply(valueof(generic));
|
||||
}
|
||||
}
|
||||
|
||||
public NumericWrapper divide(NumericWrapper wrapper) throws ArithmeticException {
|
||||
return new NumericWrapper(content.divide(wrapper.content));
|
||||
}
|
||||
|
||||
public Generic divide(Generic generic) throws ArithmeticException {
|
||||
if(generic instanceof NumericWrapper) {
|
||||
return divide((NumericWrapper)generic);
|
||||
} else {
|
||||
return divide(valueof(generic));
|
||||
}
|
||||
}
|
||||
|
||||
public Generic gcd(Generic generic) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public Generic gcd() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public Generic abs() {
|
||||
return new NumericWrapper(content.abs());
|
||||
}
|
||||
|
||||
public Generic negate() {
|
||||
return new NumericWrapper(content.negate());
|
||||
}
|
||||
|
||||
public int signum() {
|
||||
return content.signum();
|
||||
}
|
||||
|
||||
public int degree() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public Generic antiderivative(Variable variable) throws NotIntegrableException {
|
||||
return null;
|
||||
}
|
||||
|
||||
public Generic derivative(Variable variable) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public Generic substitute(Variable variable, Generic generic) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public Generic expand() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public Generic factorize() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public Generic elementary() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public Generic simplify() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public Generic numeric() {
|
||||
return this;
|
||||
}
|
||||
|
||||
public NumericWrapper valueof(NumericWrapper wrapper) {
|
||||
return new NumericWrapper(content.valueof(wrapper.content));
|
||||
}
|
||||
|
||||
public Generic valueof(Generic generic) {
|
||||
if(generic instanceof NumericWrapper) {
|
||||
return valueof((NumericWrapper)generic);
|
||||
} else if(generic instanceof JSCLInteger) {
|
||||
return new NumericWrapper((JSCLInteger)generic);
|
||||
} else throw new ArithmeticException();
|
||||
}
|
||||
|
||||
public Generic[] sumValue() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public Generic[] productValue() throws NotProductException {
|
||||
return null;
|
||||
}
|
||||
|
||||
public Power powerValue() throws NotPowerException {
|
||||
return null;
|
||||
}
|
||||
|
||||
public Expression expressionValue() throws NotExpressionException {
|
||||
throw new NotExpressionException();
|
||||
}
|
||||
|
||||
public JSCLInteger integerValue() throws NotIntegerException {
|
||||
if(content instanceof JSCLDouble) return JSCLInteger.valueOf((int)((JSCLDouble)content).doubleValue());
|
||||
else throw new NotIntegerException();
|
||||
}
|
||||
|
||||
public Variable variableValue() throws NotVariableException {
|
||||
throw new NotVariableException();
|
||||
}
|
||||
|
||||
public Variable[] variables() {
|
||||
return new Variable[0];
|
||||
}
|
||||
|
||||
public boolean isPolynomial(Variable variable) {
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean isConstant(Variable variable) {
|
||||
return true;
|
||||
}
|
||||
|
||||
public Generic sgn() {
|
||||
return new NumericWrapper(content.sgn());
|
||||
}
|
||||
|
||||
public Generic log() {
|
||||
return new NumericWrapper(content.log());
|
||||
}
|
||||
|
||||
public Generic exp() {
|
||||
return new NumericWrapper(content.exp());
|
||||
}
|
||||
|
||||
public Generic pow(Generic generic) {
|
||||
return new NumericWrapper(content.pow(((NumericWrapper)generic).content));
|
||||
}
|
||||
|
||||
public Generic sqrt() {
|
||||
return new NumericWrapper(content.sqrt());
|
||||
}
|
||||
|
||||
public Generic nthrt(int n) {
|
||||
return new NumericWrapper(content.nthrt(n));
|
||||
}
|
||||
|
||||
public static Generic root(int subscript, Generic parameter[]) {
|
||||
Numeric param[]=new Numeric[parameter.length];
|
||||
for(int i=0;i<param.length;i++) param[i]=((NumericWrapper)parameter[i]).content;
|
||||
return new NumericWrapper(Numeric.root(subscript,param));
|
||||
}
|
||||
|
||||
public Generic conjugate() {
|
||||
return new NumericWrapper(content.conjugate());
|
||||
}
|
||||
|
||||
public Generic acos() {
|
||||
return new NumericWrapper(content.acos());
|
||||
}
|
||||
|
||||
public Generic asin() {
|
||||
return new NumericWrapper(content.asin());
|
||||
}
|
||||
|
||||
public Generic atan() {
|
||||
return new NumericWrapper(content.atan());
|
||||
}
|
||||
|
||||
public Generic acot() {
|
||||
return new NumericWrapper(content.acot());
|
||||
}
|
||||
|
||||
public Generic cos() {
|
||||
return new NumericWrapper(content.cos());
|
||||
}
|
||||
|
||||
public Generic sin() {
|
||||
return new NumericWrapper(content.sin());
|
||||
}
|
||||
|
||||
public Generic tan() {
|
||||
return new NumericWrapper(content.tan());
|
||||
}
|
||||
|
||||
public Generic cot() {
|
||||
return new NumericWrapper(content.cot());
|
||||
}
|
||||
|
||||
public Generic acosh() {
|
||||
return new NumericWrapper(content.acosh());
|
||||
}
|
||||
|
||||
public Generic asinh() {
|
||||
return new NumericWrapper(content.asinh());
|
||||
}
|
||||
|
||||
public Generic atanh() {
|
||||
return new NumericWrapper(content.atanh());
|
||||
}
|
||||
|
||||
public Generic acoth() {
|
||||
return new NumericWrapper(content.acoth());
|
||||
}
|
||||
|
||||
public Generic cosh() {
|
||||
return new NumericWrapper(content.cosh());
|
||||
}
|
||||
|
||||
public Generic sinh() {
|
||||
return new NumericWrapper(content.sinh());
|
||||
}
|
||||
|
||||
public Generic tanh() {
|
||||
return new NumericWrapper(content.tanh());
|
||||
}
|
||||
|
||||
public Generic coth() {
|
||||
return new NumericWrapper(content.coth());
|
||||
}
|
||||
|
||||
public int compareTo(NumericWrapper wrapper) {
|
||||
return content.compareTo(wrapper.content);
|
||||
}
|
||||
|
||||
public int compareTo(Generic generic) {
|
||||
if(generic instanceof NumericWrapper) {
|
||||
return compareTo((NumericWrapper)generic);
|
||||
} else {
|
||||
return compareTo(valueof(generic));
|
||||
}
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return content.toString();
|
||||
}
|
||||
|
||||
public String toJava() {
|
||||
return "JSCLDouble.valueOf("+new Double(((JSCLDouble)content).doubleValue())+")";
|
||||
}
|
||||
|
||||
public void toMathML(MathML element, Object data) {
|
||||
int exponent=data instanceof Integer?((Integer)data).intValue():1;
|
||||
if(exponent==1) bodyToMathML(element);
|
||||
else {
|
||||
MathML e1=element.element("msup");
|
||||
bodyToMathML(e1);
|
||||
MathML e2=element.element("mn");
|
||||
e2.appendChild(element.text(String.valueOf(exponent)));
|
||||
e1.appendChild(e2);
|
||||
element.appendChild(e1);
|
||||
}
|
||||
}
|
||||
|
||||
void bodyToMathML(MathML element) {
|
||||
MathML e1=element.element("mn");
|
||||
e1.appendChild(element.text(String.valueOf(new Double(((JSCLDouble)content).doubleValue()))));
|
||||
element.appendChild(e1);
|
||||
}
|
||||
|
||||
protected Generic newinstance() {
|
||||
return null;
|
||||
}
|
||||
}
|
27
src/misc/src/jscl/math/Power.java
Normal file
27
src/misc/src/jscl/math/Power.java
Normal file
@ -0,0 +1,27 @@
|
||||
package jscl.math;
|
||||
|
||||
public class Power {
|
||||
final Generic value;
|
||||
final int exponent;
|
||||
|
||||
public Power(Generic value, int exponent) {
|
||||
this.value=value;
|
||||
this.exponent=exponent;
|
||||
}
|
||||
|
||||
public Generic value() {
|
||||
return value(false);
|
||||
}
|
||||
|
||||
public Generic value(boolean content) {
|
||||
return content?GenericVariable.content(value):value;
|
||||
}
|
||||
|
||||
public int exponent() {
|
||||
return exponent;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return "("+value+", "+exponent+")";
|
||||
}
|
||||
}
|
281
src/misc/src/jscl/math/Rational.java
Normal file
281
src/misc/src/jscl/math/Rational.java
Normal file
@ -0,0 +1,281 @@
|
||||
package jscl.math;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import jscl.math.function.Frac;
|
||||
import jscl.math.function.Inv;
|
||||
import jscl.mathml.MathML;
|
||||
|
||||
public final class Rational extends Generic implements Field {
|
||||
public static final Rational factory=new Rational(BigInteger.valueOf(0),BigInteger.valueOf(1));
|
||||
final BigInteger numerator;
|
||||
final BigInteger denominator;
|
||||
|
||||
public Rational(BigInteger numerator, BigInteger denominator) {
|
||||
this.numerator=numerator;
|
||||
this.denominator=denominator;
|
||||
}
|
||||
|
||||
public BigInteger numerator() {
|
||||
return numerator;
|
||||
}
|
||||
|
||||
public BigInteger denominator() {
|
||||
return denominator;
|
||||
}
|
||||
|
||||
public Rational add(Rational rational) {
|
||||
BigInteger gcd=denominator.gcd(rational.denominator);
|
||||
BigInteger c=denominator.divide(gcd);
|
||||
BigInteger c2=rational.denominator.divide(gcd);
|
||||
return new Rational(numerator.multiply(c2).add(rational.numerator.multiply(c)),denominator.multiply(c2)).reduce();
|
||||
}
|
||||
|
||||
Rational reduce() {
|
||||
BigInteger gcd=numerator.gcd(denominator);
|
||||
if(gcd.signum()!=denominator.signum()) gcd=gcd.negate();
|
||||
return gcd.signum()==0?this:new Rational(numerator.divide(gcd),denominator.divide(gcd));
|
||||
}
|
||||
|
||||
public Generic add(Generic generic) {
|
||||
if(generic instanceof Rational) {
|
||||
return add((Rational)generic);
|
||||
} else if(generic instanceof JSCLInteger) {
|
||||
return add(valueof(generic));
|
||||
} else {
|
||||
return generic.valueof(this).add(generic);
|
||||
}
|
||||
}
|
||||
|
||||
public Rational multiply(Rational rational) {
|
||||
BigInteger gcd=numerator.gcd(rational.denominator);
|
||||
BigInteger gcd2=denominator.gcd(rational.numerator);
|
||||
return new Rational(numerator.divide(gcd).multiply(rational.numerator.divide(gcd2)),denominator.divide(gcd2).multiply(rational.denominator.divide(gcd)));
|
||||
}
|
||||
|
||||
public Generic multiply(Generic generic) {
|
||||
if(generic instanceof Rational) {
|
||||
return multiply((Rational)generic);
|
||||
} else if(generic instanceof JSCLInteger) {
|
||||
return multiply(valueof(generic));
|
||||
} else {
|
||||
return generic.multiply(this);
|
||||
}
|
||||
}
|
||||
|
||||
public Generic divide(Generic generic) throws ArithmeticException {
|
||||
if(generic instanceof Rational) {
|
||||
return multiply(generic.inverse());
|
||||
} else if(generic instanceof JSCLInteger) {
|
||||
return divide(valueof(generic));
|
||||
} else {
|
||||
return generic.valueof(this).divide(generic);
|
||||
}
|
||||
}
|
||||
|
||||
public Generic inverse() {
|
||||
if(signum()<0) return new Rational(denominator.negate(),numerator.negate());
|
||||
else return new Rational(denominator,numerator);
|
||||
}
|
||||
|
||||
public Rational gcd(Rational rational) {
|
||||
return new Rational(numerator.gcd(rational.numerator),scm(denominator,rational.denominator));
|
||||
}
|
||||
|
||||
public Generic gcd(Generic generic) {
|
||||
if(generic instanceof Rational) {
|
||||
return gcd((Rational)generic);
|
||||
} else if(generic instanceof JSCLInteger) {
|
||||
return gcd(valueof(generic));
|
||||
} else {
|
||||
return generic.valueof(this).gcd(generic);
|
||||
}
|
||||
}
|
||||
|
||||
static BigInteger scm(BigInteger b1, BigInteger b2) {
|
||||
return b1.multiply(b2).divide(b1.gcd(b2));
|
||||
}
|
||||
|
||||
public Generic gcd() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public Generic pow(int exponent) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public Generic negate() {
|
||||
return new Rational(numerator.negate(),denominator);
|
||||
}
|
||||
|
||||
public int signum() {
|
||||
return numerator.signum();
|
||||
}
|
||||
|
||||
public int degree() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public Generic antiderivative(Variable variable) throws NotIntegrableException {
|
||||
return multiply(variable.expressionValue());
|
||||
}
|
||||
|
||||
public Generic derivative(Variable variable) {
|
||||
return JSCLInteger.valueOf(0);
|
||||
}
|
||||
|
||||
public Generic substitute(Variable variable, Generic generic) {
|
||||
return this;
|
||||
}
|
||||
|
||||
public Generic expand() {
|
||||
return this;
|
||||
}
|
||||
|
||||
public Generic factorize() {
|
||||
return expressionValue().factorize();
|
||||
}
|
||||
|
||||
public Generic elementary() {
|
||||
return this;
|
||||
}
|
||||
|
||||
public Generic simplify() {
|
||||
return reduce();
|
||||
}
|
||||
|
||||
public Generic numeric() {
|
||||
return new NumericWrapper(this);
|
||||
}
|
||||
|
||||
public Generic valueof(Generic generic) {
|
||||
if(generic instanceof Rational) {
|
||||
Rational r=(Rational)generic;
|
||||
return new Rational(r.numerator,r.denominator);
|
||||
} else if(generic instanceof Expression) {
|
||||
boolean sign=generic.signum()<0;
|
||||
Generic g[]=((Frac)(sign?generic.negate():generic).variableValue()).parameters();
|
||||
JSCLInteger numerator=(JSCLInteger)(sign?g[0].negate():g[0]);
|
||||
JSCLInteger denominator=(JSCLInteger)g[1];
|
||||
return new Rational(numerator.content(),denominator.content());
|
||||
} else {
|
||||
JSCLInteger en=(JSCLInteger)generic;
|
||||
return new Rational(en.content(),BigInteger.valueOf(1));
|
||||
}
|
||||
}
|
||||
|
||||
public Generic[] sumValue() {
|
||||
try {
|
||||
if(integerValue().signum()==0) return new Generic[0];
|
||||
else return new Generic[] {this};
|
||||
} catch (NotIntegerException e) {
|
||||
return new Generic[] {this};
|
||||
}
|
||||
}
|
||||
|
||||
public Generic[] productValue() throws NotProductException {
|
||||
try {
|
||||
if(integerValue().compareTo(JSCLInteger.valueOf(1))==0) return new Generic[0];
|
||||
else return new Generic[] {this};
|
||||
} catch (NotIntegerException e) {
|
||||
return new Generic[] {this};
|
||||
}
|
||||
}
|
||||
|
||||
public Power powerValue() throws NotPowerException {
|
||||
return new Power(this,1);
|
||||
}
|
||||
|
||||
public Expression expressionValue() throws NotExpressionException {
|
||||
return Expression.valueOf(this);
|
||||
}
|
||||
|
||||
public JSCLInteger integerValue() throws NotIntegerException {
|
||||
if(denominator.compareTo(BigInteger.valueOf(1))==0) return new JSCLInteger(numerator);
|
||||
else throw new NotIntegerException();
|
||||
}
|
||||
|
||||
public Variable variableValue() throws NotVariableException {
|
||||
try {
|
||||
integerValue();
|
||||
throw new NotVariableException();
|
||||
} catch (NotIntegerException e) {
|
||||
if(numerator.compareTo(BigInteger.valueOf(1))==0) return new Inv(new JSCLInteger(denominator));
|
||||
else return new Frac(new JSCLInteger(numerator), new JSCLInteger(denominator));
|
||||
}
|
||||
}
|
||||
|
||||
public Variable[] variables() {
|
||||
return new Variable[0];
|
||||
}
|
||||
|
||||
public boolean isPolynomial(Variable variable) {
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean isConstant(Variable variable) {
|
||||
return true;
|
||||
}
|
||||
|
||||
public int compareTo(Rational rational) {
|
||||
int c=denominator.compareTo(rational.denominator);
|
||||
if(c<0) return -1;
|
||||
else if(c>0) return 1;
|
||||
else return numerator.compareTo(rational.numerator);
|
||||
}
|
||||
|
||||
public int compareTo(Generic generic) {
|
||||
if(generic instanceof Rational) {
|
||||
return compareTo((Rational)generic);
|
||||
} else if(generic instanceof JSCLInteger) {
|
||||
return compareTo(valueof(generic));
|
||||
} else {
|
||||
return generic.valueof(this).compareTo(generic);
|
||||
}
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer buffer=new StringBuffer();
|
||||
try {
|
||||
buffer.append(integerValue());
|
||||
} catch (NotIntegerException e) {
|
||||
buffer.append(numerator);
|
||||
buffer.append("/");
|
||||
buffer.append(denominator);
|
||||
}
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
public String toJava() {
|
||||
return "JSCLDouble.valueOf("+numerator+"/"+denominator+")";
|
||||
}
|
||||
|
||||
public void toMathML(MathML element, Object data) {
|
||||
int exponent=data instanceof Integer?((Integer)data).intValue():1;
|
||||
if(exponent==1) bodyToMathML(element);
|
||||
else {
|
||||
MathML e1=element.element("msup");
|
||||
bodyToMathML(e1);
|
||||
MathML e2=element.element("mn");
|
||||
e2.appendChild(element.text(String.valueOf(exponent)));
|
||||
e1.appendChild(e2);
|
||||
element.appendChild(e1);
|
||||
}
|
||||
}
|
||||
|
||||
void bodyToMathML(MathML element) {
|
||||
try {
|
||||
MathML e1=element.element("mn");
|
||||
e1.appendChild(element.text(String.valueOf(integerValue())));
|
||||
element.appendChild(e1);
|
||||
} catch (NotIntegerException e) {
|
||||
MathML e1=element.element("mfrac");
|
||||
MathML e2=element.element("mn");
|
||||
e2.appendChild(element.text(String.valueOf(numerator)));
|
||||
e1.appendChild(e2);
|
||||
e2=element.element("mn");
|
||||
e2.appendChild(element.text(String.valueOf(denominator)));
|
||||
e1.appendChild(e2);
|
||||
element.appendChild(e1);
|
||||
}
|
||||
}
|
||||
}
|
226
src/misc/src/jscl/math/Simplification.java
Normal file
226
src/misc/src/jscl/math/Simplification.java
Normal file
@ -0,0 +1,226 @@
|
||||
package jscl.math;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
import jscl.math.function.Cubic;
|
||||
import jscl.math.function.Frac;
|
||||
import jscl.math.function.NotRootException;
|
||||
import jscl.math.function.Pow;
|
||||
import jscl.math.function.Root;
|
||||
import jscl.math.function.Sqrt;
|
||||
import jscl.math.polynomial.Basis;
|
||||
import jscl.math.polynomial.Monomial;
|
||||
import jscl.math.polynomial.Polynomial;
|
||||
import jscl.math.polynomial.UnivariatePolynomial;
|
||||
|
||||
public class Simplification {
|
||||
Map cache=new TreeMap();
|
||||
Generic result;
|
||||
List constraint;
|
||||
boolean linear;
|
||||
|
||||
Simplification() {}
|
||||
|
||||
public static Generic compute(Generic generic) {
|
||||
Simplification s=new Simplification();
|
||||
s.computeValue(generic);
|
||||
return s.getValue();
|
||||
}
|
||||
|
||||
void computeValue(Generic generic) {
|
||||
Debug.println("simplification");
|
||||
Debug.increment();
|
||||
Variable t=new TechnicalVariable("t");
|
||||
linear=false;
|
||||
constraint=new ArrayList();
|
||||
process(new Constraint(t,t.expressionValue().subtract(generic),false));
|
||||
UnivariatePolynomial p=polynomial(t);
|
||||
switch(p.degree()) {
|
||||
case 0:
|
||||
result=generic;
|
||||
break;
|
||||
case 1:
|
||||
result=new Root(p,0).evalsimp();
|
||||
break;
|
||||
// case 2:
|
||||
// int n=branch(generic,p);
|
||||
// if(n<p.degree()) linear(new Root(p,n).expressionValue());
|
||||
// else linear(generic);
|
||||
// break;
|
||||
default:
|
||||
linear(generic);
|
||||
}
|
||||
Debug.decrement();
|
||||
}
|
||||
|
||||
void linear(Generic generic) {
|
||||
Variable t=new TechnicalVariable("t");
|
||||
linear=true;
|
||||
constraint.clear();
|
||||
process(new Constraint(t,t.expressionValue().subtract(generic),false));
|
||||
UnivariatePolynomial p=polynomial(t);
|
||||
switch(p.degree()) {
|
||||
case 0:
|
||||
result=generic;
|
||||
break;
|
||||
default:
|
||||
result=new Root(p,0).evalsimp();
|
||||
}
|
||||
}
|
||||
|
||||
int branch(Generic generic, UnivariatePolynomial polynomial) {
|
||||
int n=polynomial.degree();
|
||||
Variable t=new TechnicalVariable("t");
|
||||
linear=true;
|
||||
for(int i=0;i<n;i++) {
|
||||
constraint.clear();
|
||||
process(new Constraint(t,t.expressionValue().subtract(generic.subtract(new Root(polynomial,i).expressionValue())),false));
|
||||
Generic a=polynomial(t).solve();
|
||||
if(a!=null?a.signum()==0:false) return i;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
UnivariatePolynomial polynomial(Variable t) {
|
||||
Polynomial fact=Polynomial.factory(t);
|
||||
int n=constraint.size();
|
||||
Generic a[]=new Generic[n];
|
||||
Variable unk[]=new Variable[n];
|
||||
if(linear) {
|
||||
int j=0;
|
||||
for(int i=0;i<n;i++) {
|
||||
Constraint c=(Constraint)constraint.get(i);
|
||||
if(c.reduce) {
|
||||
a[j]=c.generic;
|
||||
unk[j]=c.unknown;
|
||||
j++;
|
||||
}
|
||||
}
|
||||
int k=0;
|
||||
for(int i=0;i<n;i++) {
|
||||
Constraint c=(Constraint)constraint.get(i);
|
||||
if(!c.reduce) {
|
||||
a[j]=c.generic;
|
||||
unk[j]=c.unknown;
|
||||
j++;
|
||||
k++;
|
||||
}
|
||||
}
|
||||
a=solve(a,unk,k);
|
||||
for(int i=0;i<a.length;i++) {
|
||||
UnivariatePolynomial p=(UnivariatePolynomial)fact.valueof(a[i]);
|
||||
if(p.degree()==1) return p;
|
||||
}
|
||||
return null;
|
||||
} else {
|
||||
for(int i=0;i<n;i++) {
|
||||
Constraint c=(Constraint)constraint.get(i);
|
||||
a[i]=c.generic;
|
||||
unk[i]=c.unknown;
|
||||
}
|
||||
a=solve(a,unk,n);
|
||||
return (UnivariatePolynomial)fact.valueof(a[0]);
|
||||
}
|
||||
}
|
||||
|
||||
Generic[] solve(Generic generic[], Variable unknown[], int n) {
|
||||
Variable unk[]=Basis.augmentUnknown(unknown,generic);
|
||||
return Basis.compute(generic,unk,Monomial.kthElimination(n)).elements();
|
||||
}
|
||||
|
||||
void process(Constraint co) {
|
||||
int n1=0;
|
||||
int n2=0;
|
||||
constraint.add(co);
|
||||
do {
|
||||
n1=n2;
|
||||
n2=constraint.size();
|
||||
for(int i=n1;i<n2;i++) {
|
||||
co=(Constraint)constraint.get(i);
|
||||
subProcess(co);
|
||||
}
|
||||
} while(n1<n2);
|
||||
}
|
||||
|
||||
void subProcess(Constraint co) {
|
||||
Variable va[]=co.generic.variables();
|
||||
for(int i=0;i<va.length;i++) {
|
||||
Variable v=va[i];
|
||||
if(constraint.contains(new Constraint(v))) continue;
|
||||
co=null;
|
||||
if(v instanceof Frac) {
|
||||
Generic g[]=((Frac)v).parameters();
|
||||
co=new Constraint(v,v.expressionValue().multiply(g[1]).subtract(g[0]),false);
|
||||
} else if(v instanceof Sqrt) {
|
||||
Generic g[]=((Sqrt)v).parameters();
|
||||
if(linear) co=linearConstraint(v);
|
||||
if(co==null) co=new Constraint(v,v.expressionValue().pow(2).subtract(g[0]),true);
|
||||
} else if(v instanceof Cubic) {
|
||||
Generic g[]=((Cubic)v).parameters();
|
||||
if(linear) co=linearConstraint(v);
|
||||
if(co==null) co=new Constraint(v,v.expressionValue().pow(3).subtract(g[0]),true);
|
||||
} else if(v instanceof Pow) {
|
||||
try {
|
||||
Root r=((Pow)v).rootValue();
|
||||
int d=r.degree();
|
||||
Generic g[]=r.parameters();
|
||||
if(linear) co=linearConstraint(v);
|
||||
if(co==null) co=new Constraint(v,v.expressionValue().pow(d).subtract(g[0].negate()),d>1);
|
||||
} catch (NotRootException e) {
|
||||
co=linearConstraint(v);
|
||||
}
|
||||
} else if(v instanceof Root) {
|
||||
try {
|
||||
Root r=(Root)v;
|
||||
int d=r.degree();
|
||||
int n=r.subscript().integerValue().intValue();
|
||||
Generic g[]=r.parameters();
|
||||
if(linear) co=linearConstraint(v);
|
||||
if(co==null) co=new Constraint(v,Root.sigma(g,d-n).multiply(JSCLInteger.valueOf(-1).pow(d-n)).multiply(g[d]).subtract(g[n]),d>1);
|
||||
} catch (NotIntegerException e) {
|
||||
co=linearConstraint(v);
|
||||
}
|
||||
} else co=linearConstraint(v);
|
||||
if(co!=null) constraint.add(co);
|
||||
}
|
||||
}
|
||||
|
||||
Constraint linearConstraint(Variable v) {
|
||||
Generic s;
|
||||
Object o=cache.get(v);
|
||||
if(o!=null) s=(Generic)o;
|
||||
else {
|
||||
s=v.simplify();
|
||||
cache.put(v,s);
|
||||
}
|
||||
Generic a=v.expressionValue().subtract(s);
|
||||
if(a.signum()!=0) return new Constraint(v,a,false);
|
||||
else return null;
|
||||
}
|
||||
|
||||
Generic getValue() {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
class Constraint {
|
||||
Variable unknown;
|
||||
Generic generic;
|
||||
boolean reduce;
|
||||
|
||||
Constraint(Variable unknown, Generic generic, boolean reduce) {
|
||||
this.unknown=unknown;
|
||||
this.generic=generic;
|
||||
this.reduce=reduce;
|
||||
}
|
||||
|
||||
Constraint(Variable unknown) {
|
||||
this(unknown,null,false);
|
||||
}
|
||||
|
||||
public boolean equals(Object obj) {
|
||||
return unknown.compareTo(((Constraint)obj).unknown)==0;
|
||||
}
|
||||
}
|
96
src/misc/src/jscl/math/TechnicalVariable.java
Normal file
96
src/misc/src/jscl/math/TechnicalVariable.java
Normal file
@ -0,0 +1,96 @@
|
||||
package jscl.math;
|
||||
|
||||
public class TechnicalVariable extends Variable {
|
||||
public int subscript[];
|
||||
|
||||
public TechnicalVariable(String name) {
|
||||
this(name,new int[0]);
|
||||
}
|
||||
|
||||
public TechnicalVariable(String name, int subscript[]) {
|
||||
super(name);
|
||||
this.subscript=subscript;
|
||||
}
|
||||
|
||||
public Generic antiderivative(Variable variable) throws NotIntegrableException {
|
||||
throw new NotIntegrableException();
|
||||
}
|
||||
|
||||
public Generic derivative(Variable variable) {
|
||||
if(isIdentity(variable)) return JSCLInteger.valueOf(1);
|
||||
else return JSCLInteger.valueOf(0);
|
||||
}
|
||||
|
||||
public Generic substitute(Variable variable, Generic generic) {
|
||||
if(isIdentity(variable)) return generic;
|
||||
else return expressionValue();
|
||||
}
|
||||
|
||||
public Generic expand() {
|
||||
return expressionValue();
|
||||
}
|
||||
|
||||
public Generic factorize() {
|
||||
return expressionValue();
|
||||
}
|
||||
|
||||
public Generic elementary() {
|
||||
return expressionValue();
|
||||
}
|
||||
|
||||
public Generic simplify() {
|
||||
return expressionValue();
|
||||
}
|
||||
|
||||
public Generic numeric() {
|
||||
throw new ArithmeticException();
|
||||
}
|
||||
|
||||
public boolean isConstant(Variable variable) {
|
||||
return !isIdentity(variable);
|
||||
}
|
||||
|
||||
public int compareTo(Variable variable) {
|
||||
if(this==variable) return 0;
|
||||
int c=comparator.compare(this,variable);
|
||||
if(c<0) return -1;
|
||||
else if(c>0) return 1;
|
||||
else {
|
||||
TechnicalVariable v=(TechnicalVariable)variable;
|
||||
c=name.compareTo(v.name);
|
||||
if(c<0) return -1;
|
||||
else if(c>0) return 1;
|
||||
else return compareSubscript(subscript,v.subscript);
|
||||
}
|
||||
}
|
||||
|
||||
public int compareSubscript(int c1[], int c2[]) {
|
||||
if(c1.length<c2.length) return -1;
|
||||
else if(c1.length>c2.length) return 1;
|
||||
for(int i=0;i<c1.length;i++) {
|
||||
if(c1[i]<c2[i]) return -1;
|
||||
else if(c1[i]>c2[i]) return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer buffer=new StringBuffer();
|
||||
buffer.append(name);
|
||||
if(subscript.length==1) buffer.append(subscript[0]);
|
||||
else for(int i=0;i<subscript.length;i++) buffer.append("[").append(subscript[i]).append("]");
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
public String toJava() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public String toMathML(Object data) {
|
||||
return null;
|
||||
}
|
||||
|
||||
protected Variable newinstance() {
|
||||
return new TechnicalVariable(name);
|
||||
}
|
||||
}
|
188
src/misc/src/jscl/math/Variable.java
Normal file
188
src/misc/src/jscl/math/Variable.java
Normal file
@ -0,0 +1,188 @@
|
||||
package jscl.math;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import jscl.math.function.Algebraic;
|
||||
import jscl.math.function.Constant;
|
||||
import jscl.math.function.Frac;
|
||||
import jscl.math.function.Function;
|
||||
import jscl.math.function.ImplicitFunction;
|
||||
import jscl.math.function.Root;
|
||||
import jscl.math.function.Sqrt;
|
||||
import jscl.math.operator.Factorial;
|
||||
import jscl.math.operator.Operator;
|
||||
import jscl.mathml.MathML;
|
||||
import jscl.text.ParseException;
|
||||
|
||||
public abstract class Variable implements Comparable {
|
||||
public static final Comparator comparator=VariableComparator.comparator;
|
||||
protected String name;
|
||||
|
||||
public Variable(String name) {
|
||||
this.name=name;
|
||||
}
|
||||
|
||||
public String name() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public abstract Generic antiderivative(Variable variable) throws NotIntegrableException;
|
||||
public abstract Generic derivative(Variable variable);
|
||||
public abstract Generic substitute(Variable variable, Generic generic);
|
||||
public abstract Generic expand();
|
||||
public abstract Generic factorize();
|
||||
public abstract Generic elementary();
|
||||
public abstract Generic simplify();
|
||||
public abstract Generic numeric();
|
||||
|
||||
public Expression expressionValue() {
|
||||
return Expression.valueOf(this);
|
||||
}
|
||||
|
||||
public abstract boolean isConstant(Variable variable);
|
||||
|
||||
public boolean isIdentity(Variable variable) {
|
||||
return compareTo(variable)==0;
|
||||
}
|
||||
|
||||
public abstract int compareTo(Variable variable);
|
||||
|
||||
public int compareTo(Object o) {
|
||||
return compareTo((Variable)o);
|
||||
}
|
||||
|
||||
public boolean equals(Object obj) {
|
||||
if(obj instanceof Variable) {
|
||||
return compareTo((Variable)obj)==0;
|
||||
} else return false;
|
||||
}
|
||||
|
||||
public static Variable valueOf(String str) throws ParseException, NotVariableException {
|
||||
return Expression.valueOf(str).variableValue();
|
||||
}
|
||||
|
||||
public static Variable[] valueOf(String str[]) throws ParseException, NotVariableException {
|
||||
int n=str.length;
|
||||
Variable var[]=new Variable[n];
|
||||
for(int i=0;i<n;i++) var[i]=valueOf(str[i]);
|
||||
return var;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String toJava() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void toMathML(MathML element, Object data) {
|
||||
int exponent=data instanceof Integer?((Integer)data).intValue():1;
|
||||
if(exponent==1) nameToMathML(element);
|
||||
else {
|
||||
MathML e1=element.element("msup");
|
||||
nameToMathML(e1);
|
||||
MathML e2=element.element("mn");
|
||||
e2.appendChild(element.text(String.valueOf(exponent)));
|
||||
e1.appendChild(e2);
|
||||
element.appendChild(e1);
|
||||
}
|
||||
}
|
||||
|
||||
protected void nameToMathML(MathML element) {
|
||||
MathML e1=element.element("mi");
|
||||
e1.appendChild(element.text(special.containsKey(name)?(String)special.get(name):name));
|
||||
element.appendChild(e1);
|
||||
}
|
||||
|
||||
protected abstract Variable newinstance();
|
||||
|
||||
static final Map special=new HashMap();
|
||||
static {
|
||||
special.put("Alpha","\u0391");
|
||||
special.put("Beta","\u0392");
|
||||
special.put("Gamma","\u0393");
|
||||
special.put("Delta","\u0394");
|
||||
special.put("Epsilon","\u0395");
|
||||
special.put("Zeta","\u0396");
|
||||
special.put("Eta","\u0397");
|
||||
special.put("Theta","\u0398");
|
||||
special.put("Iota","\u0399");
|
||||
special.put("Kappa","\u039A");
|
||||
special.put("Lambda","\u039B");
|
||||
special.put("Mu","\u039C");
|
||||
special.put("Nu","\u039D");
|
||||
special.put("Xi","\u039E");
|
||||
special.put("Pi","\u03A0");
|
||||
special.put("Rho","\u03A1");
|
||||
special.put("Sigma","\u03A3");
|
||||
special.put("Tau","\u03A4");
|
||||
special.put("Upsilon","\u03A5");
|
||||
special.put("Phi","\u03A6");
|
||||
special.put("Chi","\u03A7");
|
||||
special.put("Psi","\u03A8");
|
||||
special.put("Omega","\u03A9");
|
||||
special.put("alpha","\u03B1");
|
||||
special.put("beta","\u03B2");
|
||||
special.put("gamma","\u03B3");
|
||||
special.put("delta","\u03B4");
|
||||
special.put("epsilon","\u03B5");
|
||||
special.put("zeta","\u03B6");
|
||||
special.put("eta","\u03B7");
|
||||
special.put("theta","\u03B8");
|
||||
special.put("iota","\u03B9");
|
||||
special.put("kappa","\u03BA");
|
||||
special.put("lambda","\u03BB");
|
||||
special.put("mu","\u03BC");
|
||||
special.put("nu","\u03BD");
|
||||
special.put("xi","\u03BE");
|
||||
special.put("pi","\u03C0");
|
||||
special.put("rho","\u03C1");
|
||||
special.put("sigma","\u03C3");
|
||||
special.put("tau","\u03C4");
|
||||
special.put("upsilon","\u03C5");
|
||||
special.put("phi","\u03C6");
|
||||
special.put("chi","\u03C7");
|
||||
special.put("psi","\u03C8");
|
||||
special.put("omega","\u03C9");
|
||||
special.put("infin","\u221E");
|
||||
special.put("nabla","\u2207");
|
||||
special.put("aleph","\u2135");
|
||||
special.put("hbar","\u210F");
|
||||
special.put("hamilt","\u210B");
|
||||
special.put("lagran","\u2112");
|
||||
special.put("square","\u25A1");
|
||||
}
|
||||
}
|
||||
|
||||
class VariableComparator implements Comparator {
|
||||
public static final Comparator comparator=new VariableComparator();
|
||||
|
||||
private VariableComparator() {}
|
||||
|
||||
public int compare(Object o1, Object o2) {
|
||||
return value((Variable)o1)-value((Variable)o2);
|
||||
}
|
||||
|
||||
static int value(Variable v) {
|
||||
int n;
|
||||
if(v instanceof TechnicalVariable) n=0;
|
||||
else if(v instanceof IntegerVariable) n=1;
|
||||
else if(v instanceof DoubleVariable) n=2;
|
||||
else if(v instanceof Frac && ((Frac)v).integer()) n=3;
|
||||
else if(v instanceof Sqrt && ((Sqrt)v).imaginary()) n=4;
|
||||
else if(v instanceof Constant) n=5;
|
||||
else if(v instanceof Root) n=6;
|
||||
else if(v instanceof Algebraic) n=7;
|
||||
else if(v instanceof ImplicitFunction) n=8;
|
||||
else if(v instanceof Function) n=9;
|
||||
else if(v instanceof Factorial) n=10;
|
||||
else if(v instanceof Operator) n=11;
|
||||
else if(v instanceof ExpressionVariable) n=12;
|
||||
else if(v instanceof VectorVariable) n=13;
|
||||
else if(v instanceof MatrixVariable) n=14;
|
||||
else throw new ArithmeticException();
|
||||
return n;
|
||||
}
|
||||
}
|
11
src/misc/src/jscl/math/VectorVariable.java
Normal file
11
src/misc/src/jscl/math/VectorVariable.java
Normal file
@ -0,0 +1,11 @@
|
||||
package jscl.math;
|
||||
|
||||
public class VectorVariable extends GenericVariable {
|
||||
public VectorVariable(Generic generic) {
|
||||
super(generic);
|
||||
}
|
||||
|
||||
protected Variable newinstance() {
|
||||
return new VectorVariable(null);
|
||||
}
|
||||
}
|
95
src/misc/src/jscl/math/function/Abs.java
Normal file
95
src/misc/src/jscl/math/function/Abs.java
Normal file
@ -0,0 +1,95 @@
|
||||
package jscl.math.function;
|
||||
|
||||
import jscl.math.Generic;
|
||||
import jscl.math.JSCLInteger;
|
||||
import jscl.math.NotIntegerException;
|
||||
import jscl.math.NotIntegrableException;
|
||||
import jscl.math.NotVariableException;
|
||||
import jscl.math.NumericWrapper;
|
||||
import jscl.math.Variable;
|
||||
import jscl.mathml.MathML;
|
||||
|
||||
public class Abs extends Function {
|
||||
public Abs(Generic generic) {
|
||||
super("abs",new Generic[] {generic});
|
||||
}
|
||||
|
||||
public Generic antiderivative(int n) throws NotIntegrableException {
|
||||
return Constant.half.multiply(parameter[0]).multiply(new Abs(parameter[0]).evaluate());
|
||||
}
|
||||
|
||||
public Generic derivative(int n) {
|
||||
return new Sgn(parameter[0]).evaluate();
|
||||
}
|
||||
|
||||
public Generic evaluate() {
|
||||
if(parameter[0].signum()<0) {
|
||||
return new Abs(parameter[0].negate()).evaluate();
|
||||
}
|
||||
try {
|
||||
return parameter[0].integerValue().abs();
|
||||
} catch (NotIntegerException e) {}
|
||||
return expressionValue();
|
||||
}
|
||||
|
||||
public Generic evalelem() {
|
||||
return new Sqrt(
|
||||
parameter[0].pow(2)
|
||||
).evalelem();
|
||||
}
|
||||
|
||||
public Generic evalsimp() {
|
||||
if(parameter[0].signum()<0) {
|
||||
return new Abs(parameter[0].negate()).evalsimp();
|
||||
}
|
||||
try {
|
||||
return parameter[0].integerValue().abs();
|
||||
} catch (NotIntegerException e) {}
|
||||
try {
|
||||
Variable v=parameter[0].variableValue();
|
||||
if(v instanceof Abs) {
|
||||
Function f=(Function)v;
|
||||
return f.evalsimp();
|
||||
} else if(v instanceof Sgn) {
|
||||
return JSCLInteger.valueOf(1);
|
||||
}
|
||||
} catch (NotVariableException e) {}
|
||||
return expressionValue();
|
||||
}
|
||||
|
||||
public Generic evalnum() {
|
||||
return ((NumericWrapper)parameter[0]).abs();
|
||||
}
|
||||
|
||||
public String toJava() {
|
||||
StringBuffer buffer=new StringBuffer();
|
||||
buffer.append(parameter[0].toJava());
|
||||
buffer.append(".abs()");
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
public void toMathML(MathML element, Object data) {
|
||||
int exponent=data instanceof Integer?((Integer)data).intValue():1;
|
||||
if(exponent==1) bodyToMathML(element);
|
||||
else {
|
||||
MathML e1=element.element("msup");
|
||||
bodyToMathML(e1);
|
||||
MathML e2=element.element("mn");
|
||||
e2.appendChild(element.text(String.valueOf(exponent)));
|
||||
e1.appendChild(e2);
|
||||
element.appendChild(e1);
|
||||
}
|
||||
}
|
||||
|
||||
void bodyToMathML(MathML element) {
|
||||
MathML e1=element.element("mfenced");
|
||||
e1.setAttribute("open","|");
|
||||
e1.setAttribute("close","|");
|
||||
parameter[0].toMathML(e1,null);
|
||||
element.appendChild(e1);
|
||||
}
|
||||
|
||||
protected Variable newinstance() {
|
||||
return new Abs(null);
|
||||
}
|
||||
}
|
32
src/misc/src/jscl/math/function/Algebraic.java
Normal file
32
src/misc/src/jscl/math/function/Algebraic.java
Normal file
@ -0,0 +1,32 @@
|
||||
package jscl.math.function;
|
||||
|
||||
import jscl.math.Generic;
|
||||
import jscl.math.NotIntegrableException;
|
||||
import jscl.mathml.MathML;
|
||||
|
||||
public abstract class Algebraic extends Function {
|
||||
public Algebraic(String name, Generic parameter[]) {
|
||||
super(name,parameter);
|
||||
}
|
||||
|
||||
public abstract Root rootValue() throws NotRootException;
|
||||
|
||||
public Generic antiderivative(int n) throws NotIntegrableException {
|
||||
return null;
|
||||
}
|
||||
|
||||
public void toMathML(MathML element, Object data) {
|
||||
int exponent=data instanceof Integer?((Integer)data).intValue():1;
|
||||
if(exponent==1) bodyToMathML(element,false);
|
||||
else {
|
||||
MathML e1=element.element("msup");
|
||||
bodyToMathML(e1,true);
|
||||
MathML e2=element.element("mn");
|
||||
e2.appendChild(element.text(String.valueOf(exponent)));
|
||||
e1.appendChild(e2);
|
||||
element.appendChild(e1);
|
||||
}
|
||||
}
|
||||
|
||||
abstract void bodyToMathML(MathML element, boolean fenced);
|
||||
}
|
18
src/misc/src/jscl/math/function/ArcTrigonometric.java
Normal file
18
src/misc/src/jscl/math/function/ArcTrigonometric.java
Normal file
@ -0,0 +1,18 @@
|
||||
package jscl.math.function;
|
||||
|
||||
import jscl.math.Generic;
|
||||
import jscl.math.NotIntegrableException;
|
||||
|
||||
public abstract class ArcTrigonometric extends Function {
|
||||
public ArcTrigonometric(String name, Generic parameter[]) {
|
||||
super(name,parameter);
|
||||
}
|
||||
|
||||
public Generic antiderivative(int n) throws NotIntegrableException {
|
||||
throw new NotIntegrableException();
|
||||
}
|
||||
|
||||
public Generic evalsimp() {
|
||||
return evaluate();
|
||||
}
|
||||
}
|
97
src/misc/src/jscl/math/function/Comparison.java
Normal file
97
src/misc/src/jscl/math/function/Comparison.java
Normal file
@ -0,0 +1,97 @@
|
||||
package jscl.math.function;
|
||||
|
||||
import jscl.math.Generic;
|
||||
import jscl.math.JSCLInteger;
|
||||
import jscl.math.NotIntegerException;
|
||||
import jscl.math.NotIntegrableException;
|
||||
import jscl.math.NumericWrapper;
|
||||
import jscl.math.Variable;
|
||||
import jscl.mathml.MathML;
|
||||
|
||||
public class Comparison extends Function {
|
||||
int operator;
|
||||
|
||||
public Comparison(String name, Generic expression1, Generic expression2) {
|
||||
super(name,new Generic[] {expression1,expression2});
|
||||
for(int i=0;i<easo.length;i++) if(name.compareTo(easo[i])==0) operator=i;
|
||||
}
|
||||
|
||||
public Generic antiderivative(int n) throws NotIntegrableException {
|
||||
throw new NotIntegrableException();
|
||||
}
|
||||
|
||||
public Generic derivative(int n) {
|
||||
return JSCLInteger.valueOf(0);
|
||||
}
|
||||
|
||||
public Generic evaluate() {
|
||||
try {
|
||||
return compare(parameter[0].integerValue(),parameter[1].integerValue());
|
||||
} catch (NotIntegerException e) {}
|
||||
return expressionValue();
|
||||
}
|
||||
|
||||
public Generic evalelem() {
|
||||
return expressionValue();
|
||||
}
|
||||
|
||||
public Generic evalsimp() {
|
||||
return expressionValue();
|
||||
}
|
||||
|
||||
public Generic evalnum() {
|
||||
return compare((NumericWrapper)parameter[0],(NumericWrapper)parameter[1]);
|
||||
}
|
||||
|
||||
JSCLInteger compare(JSCLInteger a1, JSCLInteger a2) {
|
||||
return JSCLInteger.valueOf(compare((Generic)a1,(Generic)a2)?1:0);
|
||||
}
|
||||
|
||||
NumericWrapper compare(NumericWrapper a1, NumericWrapper a2) {
|
||||
return new NumericWrapper(JSCLInteger.valueOf(compare((Generic)a1,(Generic)a2)?1:0));
|
||||
}
|
||||
|
||||
boolean compare(Generic a1, Generic a2) {
|
||||
switch(operator) {
|
||||
case 0:
|
||||
return a1.compareTo(a2)==0;
|
||||
case 1:
|
||||
return a1.compareTo(a2)<0;
|
||||
case 2:
|
||||
return a1.compareTo(a2)>0;
|
||||
case 3:
|
||||
return a1.compareTo(a2)!=0;
|
||||
case 4:
|
||||
return a1.compareTo(a2)<=0;
|
||||
case 5:
|
||||
return a1.compareTo(a2)>=0;
|
||||
case 6:
|
||||
return a1.compareTo(a2)==0;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public String toJava() {
|
||||
StringBuffer buffer=new StringBuffer();
|
||||
buffer.append(parameter[0].toJava()).append(easj[operator]).append(parameter[1].toJava());
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
public void toMathML(MathML element, Object data) {
|
||||
parameter[0].toMathML(element,null);
|
||||
MathML e1=element.element("mo");
|
||||
e1.appendChild(element.text(easm[operator]));
|
||||
element.appendChild(e1);
|
||||
parameter[1].toMathML(element,null);
|
||||
}
|
||||
|
||||
protected Variable newinstance() {
|
||||
return new Comparison(name,null,null);
|
||||
}
|
||||
|
||||
private static final String eass[]={"=","<=",">=","<>","<",">","~"};
|
||||
private static final String easj[]={"==","<=",">=","!=","<",">","=="};
|
||||
private static final String easm[]={"=","\u2264","\u2265","\u2260","<",">","\u2248"};
|
||||
private static final String easo[]={"eq","le","ge","ne","lt","gt","ap"};
|
||||
}
|
132
src/misc/src/jscl/math/function/Conjugate.java
Normal file
132
src/misc/src/jscl/math/function/Conjugate.java
Normal file
@ -0,0 +1,132 @@
|
||||
package jscl.math.function;
|
||||
|
||||
import jscl.math.Generic;
|
||||
import jscl.math.JSCLInteger;
|
||||
import jscl.math.JSCLVector;
|
||||
import jscl.math.Matrix;
|
||||
import jscl.math.NotIntegerException;
|
||||
import jscl.math.NotIntegrableException;
|
||||
import jscl.math.NotVariableException;
|
||||
import jscl.math.NumericWrapper;
|
||||
import jscl.math.Power;
|
||||
import jscl.math.Variable;
|
||||
import jscl.mathml.MathML;
|
||||
|
||||
public class Conjugate extends Function {
|
||||
public Conjugate(Generic generic) {
|
||||
super("conjugate",new Generic[] {generic});
|
||||
}
|
||||
|
||||
public Generic antiderivative(int n) throws NotIntegrableException {
|
||||
return Constant.half.multiply(evaluate().pow(2));
|
||||
}
|
||||
|
||||
public Generic derivative(int n) {
|
||||
return JSCLInteger.valueOf(1);
|
||||
}
|
||||
|
||||
public Generic evaluate() {
|
||||
try {
|
||||
return parameter[0].integerValue();
|
||||
} catch (NotIntegerException e) {}
|
||||
if(parameter[0] instanceof Matrix) {
|
||||
return ((Matrix)parameter[0]).conjugate();
|
||||
} else if(parameter[0] instanceof JSCLVector) {
|
||||
return ((JSCLVector)parameter[0]).conjugate();
|
||||
}
|
||||
return expressionValue();
|
||||
}
|
||||
|
||||
public Generic evalelem() {
|
||||
try {
|
||||
return parameter[0].integerValue();
|
||||
} catch (NotIntegerException e) {}
|
||||
return expressionValue();
|
||||
}
|
||||
|
||||
public Generic evalsimp() {
|
||||
try {
|
||||
return parameter[0].integerValue();
|
||||
} catch (NotIntegerException e) {}
|
||||
if(parameter[0].signum()<0) {
|
||||
return new Conjugate(parameter[0].negate()).evalsimp().negate();
|
||||
} else if(parameter[0].compareTo(Constant.i)==0) {
|
||||
return Constant.i.negate();
|
||||
}
|
||||
try {
|
||||
Variable v=parameter[0].variableValue();
|
||||
if(v instanceof Conjugate) {
|
||||
Generic g[]=((Conjugate)v).parameters();
|
||||
return g[0];
|
||||
} else if(v instanceof Exp) {
|
||||
Generic g[]=((Exp)v).parameters();
|
||||
return new Exp(new Conjugate(g[0]).evalsimp()).evalsimp();
|
||||
} else if(v instanceof Log) {
|
||||
Generic g[]=((Log)v).parameters();
|
||||
return new Log(new Conjugate(g[0]).evalsimp()).evalsimp();
|
||||
}
|
||||
} catch (NotVariableException e) {
|
||||
Generic a[]=parameter[0].sumValue();
|
||||
if(a.length>1) {
|
||||
Generic s=JSCLInteger.valueOf(0);
|
||||
for(int i=0;i<a.length;i++) {
|
||||
s=s.add(new Conjugate(a[i]).evalsimp());
|
||||
}
|
||||
return s;
|
||||
} else {
|
||||
Generic p[]=a[0].productValue();
|
||||
Generic s=JSCLInteger.valueOf(1);
|
||||
for(int i=0;i<p.length;i++) {
|
||||
Power o=p[i].powerValue();
|
||||
s=s.multiply(new Conjugate(o.value()).evalsimp().pow(o.exponent()));
|
||||
}
|
||||
return s;
|
||||
}
|
||||
}
|
||||
Generic n[]=Frac.separateCoefficient(parameter[0]);
|
||||
if(n[0].compareTo(JSCLInteger.valueOf(1))==0 && n[1].compareTo(JSCLInteger.valueOf(1))==0);
|
||||
else return new Conjugate(n[2]).evalsimp().multiply(
|
||||
new Frac(n[0],n[1]).evalsimp()
|
||||
);
|
||||
return expressionValue();
|
||||
}
|
||||
|
||||
public Generic evalnum() {
|
||||
return ((NumericWrapper)parameter[0]).conjugate();
|
||||
}
|
||||
|
||||
public String toJava() {
|
||||
StringBuffer buffer=new StringBuffer();
|
||||
buffer.append(parameter[0].toJava());
|
||||
buffer.append(".conjugate()");
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
public void toMathML(MathML element, Object data) {
|
||||
int exponent=data instanceof Integer?((Integer)data).intValue():1;
|
||||
if(exponent==1) bodyToMathML(element);
|
||||
else {
|
||||
MathML e1=element.element("msup");
|
||||
MathML e2=element.element("mfenced");
|
||||
bodyToMathML(e2);
|
||||
e1.appendChild(e2);
|
||||
e2=element.element("mn");
|
||||
e2.appendChild(element.text(String.valueOf(exponent)));
|
||||
e1.appendChild(e2);
|
||||
element.appendChild(e1);
|
||||
}
|
||||
}
|
||||
|
||||
void bodyToMathML(MathML element) {
|
||||
MathML e1=element.element("mover");
|
||||
parameter[0].toMathML(e1,null);
|
||||
MathML e2=element.element("mo");
|
||||
e2.appendChild(element.text("_"));
|
||||
e1.appendChild(e2);
|
||||
element.appendChild(e1);
|
||||
}
|
||||
|
||||
protected Variable newinstance() {
|
||||
return new Conjugate(null);
|
||||
}
|
||||
}
|
229
src/misc/src/jscl/math/function/Constant.java
Normal file
229
src/misc/src/jscl/math/function/Constant.java
Normal file
@ -0,0 +1,229 @@
|
||||
package jscl.math.function;
|
||||
|
||||
import jscl.math.Generic;
|
||||
import jscl.math.JSCLInteger;
|
||||
import jscl.math.NotIntegrableException;
|
||||
import jscl.math.NumericWrapper;
|
||||
import jscl.math.Variable;
|
||||
import jscl.mathml.MathML;
|
||||
import jscl.util.ArrayComparator;
|
||||
|
||||
public class Constant extends Variable {
|
||||
public static final Generic e=new Exp(JSCLInteger.valueOf(1)).expressionValue();
|
||||
public static final Generic pi=new Constant("pi").expressionValue();
|
||||
public static final Generic i=new Sqrt(JSCLInteger.valueOf(-1)).expressionValue();
|
||||
public static final Generic half=new Inv(JSCLInteger.valueOf(2)).expressionValue();
|
||||
public static final Generic third=new Inv(JSCLInteger.valueOf(3)).expressionValue();
|
||||
public static final Generic j=half.negate().multiply(JSCLInteger.valueOf(1).subtract(i.multiply(new Sqrt(JSCLInteger.valueOf(3)).expressionValue())));
|
||||
public static final Generic jbar=half.negate().multiply(JSCLInteger.valueOf(1).add(i.multiply(new Sqrt(JSCLInteger.valueOf(3)).expressionValue())));
|
||||
public static final Generic infinity=new Constant("infin").expressionValue();
|
||||
static final int PRIMECHARS=3;
|
||||
protected int prime;
|
||||
protected Generic subscript[];
|
||||
|
||||
public Constant(String name) {
|
||||
this(name,0,new Generic[0]);
|
||||
}
|
||||
|
||||
public Constant(String name, int prime, Generic subscript[]) {
|
||||
super(name);
|
||||
this.prime=prime;
|
||||
this.subscript=subscript;
|
||||
}
|
||||
|
||||
public int prime() {
|
||||
return prime;
|
||||
}
|
||||
|
||||
public Generic[] subscript() {
|
||||
return subscript;
|
||||
}
|
||||
|
||||
public Generic antiderivative(Variable variable) throws NotIntegrableException {
|
||||
return null;
|
||||
}
|
||||
|
||||
public Generic derivative(Variable variable) {
|
||||
if(isIdentity(variable)) return JSCLInteger.valueOf(1);
|
||||
else return JSCLInteger.valueOf(0);
|
||||
}
|
||||
|
||||
public Generic substitute(Variable variable, Generic generic) {
|
||||
Constant v=(Constant)newinstance();
|
||||
for(int i=0;i<subscript.length;i++) {
|
||||
v.subscript[i]=subscript[i].substitute(variable,generic);
|
||||
}
|
||||
if(v.isIdentity(variable)) return generic;
|
||||
else return v.expressionValue();
|
||||
}
|
||||
|
||||
public Generic expand() {
|
||||
Constant v=(Constant)newinstance();
|
||||
for(int i=0;i<subscript.length;i++) {
|
||||
v.subscript[i]=subscript[i].expand();
|
||||
}
|
||||
return v.expressionValue();
|
||||
}
|
||||
|
||||
public Generic factorize() {
|
||||
Constant v=(Constant)newinstance();
|
||||
for(int i=0;i<subscript.length;i++) {
|
||||
v.subscript[i]=subscript[i].factorize();
|
||||
}
|
||||
return v.expressionValue();
|
||||
}
|
||||
|
||||
public Generic elementary() {
|
||||
Constant v=(Constant)newinstance();
|
||||
for(int i=0;i<subscript.length;i++) {
|
||||
v.subscript[i]=subscript[i].elementary();
|
||||
}
|
||||
return v.expressionValue();
|
||||
}
|
||||
|
||||
public Generic simplify() {
|
||||
Constant v=(Constant)newinstance();
|
||||
for(int i=0;i<subscript.length;i++) {
|
||||
v.subscript[i]=subscript[i].simplify();
|
||||
}
|
||||
return v.expressionValue();
|
||||
}
|
||||
|
||||
public Generic numeric() {
|
||||
return new NumericWrapper(this);
|
||||
}
|
||||
|
||||
public boolean isConstant(Variable variable) {
|
||||
return !isIdentity(variable);
|
||||
}
|
||||
|
||||
public int compareTo(Variable variable) {
|
||||
if(this==variable) return 0;
|
||||
int c=comparator.compare(this,variable);
|
||||
if(c<0) return -1;
|
||||
else if(c>0) return 1;
|
||||
else {
|
||||
Constant v=(Constant)variable;
|
||||
c=name.compareTo(v.name);
|
||||
if(c<0) return -1;
|
||||
else if(c>0) return 1;
|
||||
else {
|
||||
c=ArrayComparator.comparator.compare(subscript,v.subscript);
|
||||
if(c<0) return -1;
|
||||
else if(c>0) return 1;
|
||||
else {
|
||||
if(prime<v.prime) return -1;
|
||||
else if(prime>v.prime) return 1;
|
||||
else return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer buffer=new StringBuffer();
|
||||
buffer.append(name);
|
||||
for(int i=0;i<subscript.length;i++) {
|
||||
buffer.append("[").append(subscript[i]).append("]");
|
||||
}
|
||||
if(prime==0);
|
||||
else if(prime<=PRIMECHARS) buffer.append(primechars(prime));
|
||||
else buffer.append("{").append(prime).append("}");
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
static String primechars(int n) {
|
||||
StringBuffer buffer=new StringBuffer();
|
||||
for(int i=0;i<n;i++) buffer.append("'");
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
public String toJava() {
|
||||
if(compareTo(new Constant("pi"))==0) return "JSCLDouble.valueOf(Math.PI)";
|
||||
else if(compareTo(new Constant("infin"))==0) return "JSCLDouble.valueOf(Double.POSITIVE_INFINITY)";
|
||||
StringBuffer buffer=new StringBuffer();
|
||||
buffer.append(name);
|
||||
if(prime==0);
|
||||
else if(prime<=PRIMECHARS) buffer.append(underscores(prime));
|
||||
else buffer.append("_").append(prime);
|
||||
for(int i=0;i<subscript.length;i++) {
|
||||
buffer.append("[").append(subscript[i].integerValue().intValue()).append("]");
|
||||
}
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
static String underscores(int n) {
|
||||
StringBuffer buffer=new StringBuffer();
|
||||
for(int i=0;i<n;i++) buffer.append("_");
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
public void toMathML(MathML element, Object data) {
|
||||
int exponent=data instanceof Integer?((Integer)data).intValue():1;
|
||||
if(exponent==1) bodyToMathML(element);
|
||||
else {
|
||||
MathML e1=element.element("msup");
|
||||
bodyToMathML(e1);
|
||||
MathML e2=element.element("mn");
|
||||
e2.appendChild(element.text(String.valueOf(exponent)));
|
||||
e1.appendChild(e2);
|
||||
element.appendChild(e1);
|
||||
}
|
||||
}
|
||||
|
||||
public void bodyToMathML(MathML element) {
|
||||
if(subscript.length==0) {
|
||||
if(prime==0) {
|
||||
nameToMathML(element);
|
||||
} else {
|
||||
MathML e1=element.element("msup");
|
||||
nameToMathML(e1);
|
||||
primeToMathML(e1);
|
||||
element.appendChild(e1);
|
||||
}
|
||||
} else {
|
||||
if(prime==0) {
|
||||
MathML e1=element.element("msub");
|
||||
nameToMathML(e1);
|
||||
MathML e2=element.element("mrow");
|
||||
for(int i=0;i<subscript.length;i++) {
|
||||
subscript[i].toMathML(e2,null);
|
||||
}
|
||||
e1.appendChild(e2);
|
||||
element.appendChild(e1);
|
||||
} else {
|
||||
MathML e1=element.element("msubsup");
|
||||
nameToMathML(e1);
|
||||
MathML e2=element.element("mrow");
|
||||
for(int i=0;i<subscript.length;i++) {
|
||||
subscript[i].toMathML(e2,null);
|
||||
}
|
||||
e1.appendChild(e2);
|
||||
primeToMathML(e1);
|
||||
element.appendChild(e1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void primeToMathML(MathML element) {
|
||||
if(prime<=PRIMECHARS) {
|
||||
primecharsToMathML(element,prime);
|
||||
} else {
|
||||
MathML e1=element.element("mfenced");
|
||||
MathML e2=element.element("mn");
|
||||
e2.appendChild(element.text(String.valueOf(prime)));
|
||||
e1.appendChild(e2);
|
||||
element.appendChild(e1);
|
||||
}
|
||||
}
|
||||
|
||||
static void primecharsToMathML(MathML element, int n) {
|
||||
MathML e1=element.element("mo");
|
||||
for(int i=0;i<n;i++) e1.appendChild(element.text("\u2032"));
|
||||
element.appendChild(e1);
|
||||
}
|
||||
|
||||
protected Variable newinstance() {
|
||||
return new Constant(name,prime,new Generic[subscript.length]);
|
||||
}
|
||||
}
|
96
src/misc/src/jscl/math/function/Cubic.java
Normal file
96
src/misc/src/jscl/math/function/Cubic.java
Normal file
@ -0,0 +1,96 @@
|
||||
package jscl.math.function;
|
||||
|
||||
import jscl.math.Antiderivative;
|
||||
import jscl.math.Generic;
|
||||
import jscl.math.JSCLInteger;
|
||||
import jscl.math.NotIntegerException;
|
||||
import jscl.math.NotIntegrableException;
|
||||
import jscl.math.NumericWrapper;
|
||||
import jscl.math.Variable;
|
||||
import jscl.mathml.MathML;
|
||||
|
||||
public class Cubic extends Algebraic {
|
||||
public Cubic(Generic generic) {
|
||||
super("cubic",new Generic[] {generic});
|
||||
}
|
||||
|
||||
public Root rootValue() {
|
||||
return new Root(
|
||||
new Generic[] {
|
||||
parameter[0].negate(),
|
||||
JSCLInteger.valueOf(0),
|
||||
JSCLInteger.valueOf(0),
|
||||
JSCLInteger.valueOf(1)
|
||||
},
|
||||
0
|
||||
);
|
||||
}
|
||||
|
||||
public Generic antiderivative(Variable variable) throws NotIntegrableException {
|
||||
Root r=rootValue();
|
||||
Generic g[]=r.parameters();
|
||||
if(g[0].isPolynomial(variable)) {
|
||||
return Antiderivative.compute(r,variable);
|
||||
} else throw new NotIntegrableException();
|
||||
}
|
||||
|
||||
public Generic derivative(int n) {
|
||||
return Constant.third.multiply(
|
||||
new Inv(
|
||||
evaluate().pow(2)
|
||||
).evaluate()
|
||||
);
|
||||
}
|
||||
|
||||
public Generic evaluate() {
|
||||
try {
|
||||
JSCLInteger en=parameter[0].integerValue();
|
||||
if(en.signum()<0);
|
||||
else {
|
||||
Generic rt=en.nthrt(3);
|
||||
if(rt.pow(3).compareTo(en)==0) return rt;
|
||||
}
|
||||
} catch (NotIntegerException e) {}
|
||||
return expressionValue();
|
||||
}
|
||||
|
||||
public Generic evalelem() {
|
||||
return evaluate();
|
||||
}
|
||||
|
||||
public Generic evalsimp() {
|
||||
try {
|
||||
JSCLInteger en=parameter[0].integerValue();
|
||||
if(en.signum()<0) return new Cubic(en.negate()).evalsimp().negate();
|
||||
else {
|
||||
Generic rt=en.nthrt(3);
|
||||
if(rt.pow(3).compareTo(en)==0) return rt;
|
||||
}
|
||||
} catch (NotIntegerException e) {}
|
||||
return expressionValue();
|
||||
}
|
||||
|
||||
public Generic evalnum() {
|
||||
return ((NumericWrapper)parameter[0]).nthrt(3);
|
||||
}
|
||||
|
||||
public String toJava() {
|
||||
StringBuffer buffer=new StringBuffer();
|
||||
buffer.append(parameter[0].toJava());
|
||||
buffer.append(".pow(");
|
||||
buffer.append(Constant.third.toJava());
|
||||
buffer.append(")");
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
void bodyToMathML(MathML element, boolean fenced) {
|
||||
MathML e1=element.element("mroot");
|
||||
parameter[0].toMathML(e1,null);
|
||||
JSCLInteger.valueOf(3).toMathML(e1,null);
|
||||
element.appendChild(e1);
|
||||
}
|
||||
|
||||
protected Variable newinstance() {
|
||||
return new Cubic(null);
|
||||
}
|
||||
}
|
121
src/misc/src/jscl/math/function/Exp.java
Normal file
121
src/misc/src/jscl/math/function/Exp.java
Normal file
@ -0,0 +1,121 @@
|
||||
package jscl.math.function;
|
||||
|
||||
import jscl.math.Generic;
|
||||
import jscl.math.JSCLInteger;
|
||||
import jscl.math.NotIntegrableException;
|
||||
import jscl.math.NotVariableException;
|
||||
import jscl.math.NumericWrapper;
|
||||
import jscl.math.Variable;
|
||||
import jscl.math.polynomial.Polynomial;
|
||||
import jscl.mathml.MathML;
|
||||
|
||||
public class Exp extends Function {
|
||||
public Exp(Generic generic) {
|
||||
super("exp",new Generic[] {generic});
|
||||
}
|
||||
|
||||
public Generic antiderivative(Variable variable) throws NotIntegrableException {
|
||||
Generic s=parameter[0];
|
||||
if(s.isPolynomial(variable)) {
|
||||
Polynomial p=Polynomial.factory(variable).valueof(s);
|
||||
if(p.degree()==1) {
|
||||
Generic a[]=p.elements();
|
||||
return new Inv(a[1]).evaluate().multiply(antiderivative(0));
|
||||
} else throw new NotIntegrableException();
|
||||
} else throw new NotIntegrableException();
|
||||
}
|
||||
|
||||
public Generic antiderivative(int n) throws NotIntegrableException {
|
||||
return evaluate();
|
||||
}
|
||||
|
||||
public Generic derivative(int n) {
|
||||
return evaluate();
|
||||
}
|
||||
|
||||
public Generic evaluate() {
|
||||
if(parameter[0].signum()<0) {
|
||||
return new Inv(new Exp(parameter[0].negate()).evaluate()).evaluate();
|
||||
} else if(parameter[0].signum()==0) {
|
||||
return JSCLInteger.valueOf(1);
|
||||
}
|
||||
return expressionValue();
|
||||
}
|
||||
|
||||
public Generic evalelem() {
|
||||
return evaluate();
|
||||
}
|
||||
|
||||
public Generic evalsimp() {
|
||||
if(parameter[0].signum()<0) {
|
||||
return new Inv(new Exp(parameter[0].negate()).evalsimp()).evalsimp();
|
||||
} else if(parameter[0].signum()==0) {
|
||||
return JSCLInteger.valueOf(1);
|
||||
} else if(parameter[0].compareTo(Constant.i.multiply(Constant.pi))==0) {
|
||||
return JSCLInteger.valueOf(-1);
|
||||
}
|
||||
try {
|
||||
Variable v=parameter[0].variableValue();
|
||||
if(v instanceof Log) {
|
||||
Generic g[]=((Log)v).parameters();
|
||||
return g[0];
|
||||
}
|
||||
} catch (NotVariableException e) {
|
||||
Generic a[]=parameter[0].sumValue();
|
||||
if(a.length>1) {
|
||||
Generic s=JSCLInteger.valueOf(1);
|
||||
for(int i=0;i<a.length;i++) {
|
||||
s=s.multiply(new Exp(a[i]).evalsimp());
|
||||
}
|
||||
return s;
|
||||
}
|
||||
}
|
||||
Generic n[]=Frac.separateCoefficient(parameter[0]);
|
||||
if(n[0].compareTo(JSCLInteger.valueOf(1))==0 && n[1].compareTo(JSCLInteger.valueOf(1))==0);
|
||||
else return new Pow(
|
||||
new Exp(n[2]).evalsimp(),
|
||||
new Frac(n[0],n[1]).evalsimp()
|
||||
).evalsimp();
|
||||
return expressionValue();
|
||||
}
|
||||
|
||||
public Generic evalnum() {
|
||||
return ((NumericWrapper)parameter[0]).exp();
|
||||
}
|
||||
|
||||
public void toMathML(MathML element, Object data) {
|
||||
int exponent=data instanceof Integer?((Integer)data).intValue():1;
|
||||
if(exponent==1) bodyToMathML(element,false);
|
||||
else {
|
||||
MathML e1=element.element("msup");
|
||||
bodyToMathML(e1,true);
|
||||
MathML e2=element.element("mn");
|
||||
e2.appendChild(element.text(String.valueOf(exponent)));
|
||||
e1.appendChild(e2);
|
||||
element.appendChild(e1);
|
||||
}
|
||||
}
|
||||
|
||||
void bodyToMathML(MathML element, boolean fenced) {
|
||||
if(fenced) {
|
||||
MathML e1=element.element("mfenced");
|
||||
bodyToMathML(e1);
|
||||
element.appendChild(e1);
|
||||
} else {
|
||||
bodyToMathML(element);
|
||||
}
|
||||
}
|
||||
|
||||
void bodyToMathML(MathML element) {
|
||||
MathML e1=element.element("msup");
|
||||
MathML e2=element.element("mi");
|
||||
e2.appendChild(element.text(/*"\u2147"*/"e"));
|
||||
e1.appendChild(e2);
|
||||
parameter[0].toMathML(e1,null);
|
||||
element.appendChild(e1);
|
||||
}
|
||||
|
||||
protected Variable newinstance() {
|
||||
return new Exp(null);
|
||||
}
|
||||
}
|
162
src/misc/src/jscl/math/function/Frac.java
Normal file
162
src/misc/src/jscl/math/function/Frac.java
Normal file
@ -0,0 +1,162 @@
|
||||
package jscl.math.function;
|
||||
|
||||
import jscl.math.Antiderivative;
|
||||
import jscl.math.Generic;
|
||||
import jscl.math.GenericVariable;
|
||||
import jscl.math.JSCLInteger;
|
||||
import jscl.math.NotDivisibleException;
|
||||
import jscl.math.NotExpressionException;
|
||||
import jscl.math.NotIntegerException;
|
||||
import jscl.math.NotIntegrableException;
|
||||
import jscl.math.NotPowerException;
|
||||
import jscl.math.NotVariableException;
|
||||
import jscl.math.NumericWrapper;
|
||||
import jscl.math.Variable;
|
||||
import jscl.mathml.MathML;
|
||||
|
||||
public class Frac extends Algebraic {
|
||||
public Frac(Generic numerator, Generic denominator) {
|
||||
super("frac",new Generic[] {numerator,denominator});
|
||||
}
|
||||
|
||||
public Root rootValue() {
|
||||
return new Root(
|
||||
new Generic[] {
|
||||
parameter[0].negate(),
|
||||
parameter[1]
|
||||
},
|
||||
0
|
||||
);
|
||||
}
|
||||
|
||||
public Generic antiderivative(Variable variable) throws NotIntegrableException {
|
||||
if(parameter[0].isPolynomial(variable) && parameter[1].isPolynomial(variable)) {
|
||||
return Antiderivative.compute(this,variable);
|
||||
} else throw new NotIntegrableException();
|
||||
}
|
||||
|
||||
public Generic derivative(int n) {
|
||||
if(n==0) {
|
||||
return new Inv(parameter[1]).evaluate();
|
||||
} else {
|
||||
return parameter[0].multiply(new Inv(parameter[1]).evaluate().pow(2).negate());
|
||||
}
|
||||
}
|
||||
|
||||
public boolean integer() {
|
||||
try {
|
||||
parameter[0].integerValue().intValue();
|
||||
parameter[1].integerValue().intValue();
|
||||
return true;
|
||||
} catch (NotIntegerException e) {}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Generic evaluate() {
|
||||
if(parameter[0].compareTo(JSCLInteger.valueOf(1))==0) {
|
||||
return new Inv(parameter[1]).evaluate();
|
||||
}
|
||||
try {
|
||||
return parameter[0].divide(parameter[1]);
|
||||
} catch (NotDivisibleException e) {}
|
||||
return expressionValue();
|
||||
}
|
||||
|
||||
public Generic evalelem() {
|
||||
return evaluate();
|
||||
}
|
||||
|
||||
public Generic evalsimp() {
|
||||
if(parameter[0].signum()<0) {
|
||||
return new Frac(parameter[0].negate(),parameter[1]).evalsimp().negate();
|
||||
}
|
||||
if(parameter[1].signum()<0) {
|
||||
return new Frac(parameter[0].negate(),parameter[1].negate()).evalsimp();
|
||||
}
|
||||
return evaluate();
|
||||
}
|
||||
|
||||
public Generic evalnum() {
|
||||
return ((NumericWrapper)parameter[0]).divide((NumericWrapper)parameter[1]);
|
||||
}
|
||||
|
||||
static Generic[] separateCoefficient(Generic generic) {
|
||||
if(generic.signum()<0) {
|
||||
Generic n[]=separateCoefficient(generic.negate());
|
||||
return new Generic[] {n[0],n[1],n[2].negate()};
|
||||
}
|
||||
try {
|
||||
Variable v=generic.variableValue();
|
||||
if(v instanceof Frac) {
|
||||
Generic g[]=((Frac)v).parameters();
|
||||
Generic a=g[0].expressionValue();
|
||||
Generic d=g[1].expressionValue();
|
||||
Generic na[]=a.gcdAndNormalize();
|
||||
Generic nd[]=d.gcdAndNormalize();
|
||||
return new Generic[] {na[0],nd[0],new Frac(na[1],nd[1]).evaluate()};
|
||||
}
|
||||
} catch (NotVariableException e) {
|
||||
try {
|
||||
Generic a=generic.expressionValue();
|
||||
Generic n[]=a.gcdAndNormalize();
|
||||
return new Generic[] {n[0],JSCLInteger.valueOf(1),n[1]};
|
||||
} catch (NotExpressionException e2) {}
|
||||
}
|
||||
return new Generic[] {JSCLInteger.valueOf(1),JSCLInteger.valueOf(1),generic};
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer buffer=new StringBuffer();
|
||||
try {
|
||||
parameter[0].powerValue();
|
||||
buffer.append(parameter[0]);
|
||||
} catch (NotPowerException e) {
|
||||
buffer.append(GenericVariable.valueOf(parameter[0]));
|
||||
}
|
||||
buffer.append("/");
|
||||
try {
|
||||
Variable v=parameter[1].variableValue();
|
||||
if(v instanceof Frac) {
|
||||
buffer.append(GenericVariable.valueOf(parameter[1]));
|
||||
} else buffer.append(v);
|
||||
} catch (NotVariableException e) {
|
||||
try {
|
||||
parameter[1].abs().powerValue();
|
||||
buffer.append(parameter[1]);
|
||||
} catch (NotPowerException e2) {
|
||||
buffer.append(GenericVariable.valueOf(parameter[1]));
|
||||
}
|
||||
}
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
public String toJava() {
|
||||
StringBuffer buffer=new StringBuffer();
|
||||
buffer.append(parameter[0].toJava());
|
||||
buffer.append(".divide(");
|
||||
buffer.append(parameter[1].toJava());
|
||||
buffer.append(")");
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
void bodyToMathML(MathML element, boolean fenced) {
|
||||
if(fenced) {
|
||||
MathML e1=element.element("mfenced");
|
||||
bodyToMathML(e1);
|
||||
element.appendChild(e1);
|
||||
} else {
|
||||
bodyToMathML(element);
|
||||
}
|
||||
}
|
||||
|
||||
void bodyToMathML(MathML element) {
|
||||
MathML e1=element.element("mfrac");
|
||||
parameter[0].toMathML(e1,null);
|
||||
parameter[1].toMathML(e1,null);
|
||||
element.appendChild(e1);
|
||||
}
|
||||
|
||||
protected Variable newinstance() {
|
||||
return new Frac(null,null);
|
||||
}
|
||||
}
|
166
src/misc/src/jscl/math/function/Function.java
Normal file
166
src/misc/src/jscl/math/function/Function.java
Normal file
@ -0,0 +1,166 @@
|
||||
package jscl.math.function;
|
||||
|
||||
import jscl.math.Generic;
|
||||
import jscl.math.JSCLInteger;
|
||||
import jscl.math.NotIntegrableException;
|
||||
import jscl.math.Variable;
|
||||
import jscl.mathml.MathML;
|
||||
import jscl.util.ArrayComparator;
|
||||
|
||||
public abstract class Function extends Variable {
|
||||
protected Generic parameter[];
|
||||
|
||||
public Function(String name, Generic parameter[]) {
|
||||
super(name);
|
||||
this.parameter=parameter;
|
||||
}
|
||||
|
||||
public Generic[] parameters() {
|
||||
return parameter;
|
||||
}
|
||||
|
||||
public abstract Generic evaluate();
|
||||
|
||||
public abstract Generic evalelem();
|
||||
|
||||
public abstract Generic evalsimp();
|
||||
|
||||
public abstract Generic evalnum();
|
||||
|
||||
public Generic antiderivative(Variable variable) throws NotIntegrableException {
|
||||
int n=-1;
|
||||
for(int i=0;i<parameter.length;i++) {
|
||||
if(n==-1 && parameter[i].isIdentity(variable)) n=i;
|
||||
else if(parameter[i].isConstant(variable));
|
||||
else {
|
||||
n=-1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(n<0) throw new NotIntegrableException();
|
||||
else return antiderivative(n);
|
||||
}
|
||||
|
||||
public abstract Generic antiderivative(int n) throws NotIntegrableException;
|
||||
|
||||
public Generic derivative(Variable variable) {
|
||||
if(isIdentity(variable)) return JSCLInteger.valueOf(1);
|
||||
else {
|
||||
Generic a=JSCLInteger.valueOf(0);
|
||||
for(int i=0;i<parameter.length;i++) {
|
||||
a=a.add(parameter[i].derivative(variable).multiply(derivative(i)));
|
||||
}
|
||||
return a;
|
||||
}
|
||||
}
|
||||
|
||||
public abstract Generic derivative(int n);
|
||||
|
||||
public Generic substitute(Variable variable, Generic generic) {
|
||||
Function v=(Function)newinstance();
|
||||
for(int i=0;i<parameter.length;i++) {
|
||||
v.parameter[i]=parameter[i].substitute(variable,generic);
|
||||
}
|
||||
if(v.isIdentity(variable)) return generic;
|
||||
else return v.evaluate();
|
||||
}
|
||||
|
||||
public Generic expand() {
|
||||
Function v=(Function)newinstance();
|
||||
for(int i=0;i<parameter.length;i++) {
|
||||
v.parameter[i]=parameter[i].expand();
|
||||
}
|
||||
return v.evaluate();
|
||||
}
|
||||
|
||||
public Generic factorize() {
|
||||
Function v=(Function)newinstance();
|
||||
for(int i=0;i<parameter.length;i++) {
|
||||
v.parameter[i]=parameter[i].factorize();
|
||||
}
|
||||
return v.expressionValue();
|
||||
}
|
||||
|
||||
public Generic elementary() {
|
||||
Function v=(Function)newinstance();
|
||||
for(int i=0;i<parameter.length;i++) {
|
||||
v.parameter[i]=parameter[i].elementary();
|
||||
}
|
||||
return v.evalelem();
|
||||
}
|
||||
|
||||
public Generic simplify() {
|
||||
Function v=(Function)newinstance();
|
||||
for(int i=0;i<parameter.length;i++) {
|
||||
v.parameter[i]=parameter[i].simplify();
|
||||
}
|
||||
return v.evalsimp();
|
||||
}
|
||||
|
||||
public Generic numeric() {
|
||||
Function v=(Function)newinstance();
|
||||
for(int i=0;i<parameter.length;i++) {
|
||||
v.parameter[i]=parameter[i].numeric();
|
||||
}
|
||||
return v.evalnum();
|
||||
}
|
||||
|
||||
public boolean isConstant(Variable variable) {
|
||||
boolean s=!isIdentity(variable);
|
||||
for(int i=0;i<parameter.length;i++) {
|
||||
s=s && parameter[i].isConstant(variable);
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
public int compareTo(Variable variable) {
|
||||
if(this==variable) return 0;
|
||||
int c=comparator.compare(this,variable);
|
||||
if(c<0) return -1;
|
||||
else if(c>0) return 1;
|
||||
else {
|
||||
Function v=(Function)variable;
|
||||
c=name.compareTo(v.name);
|
||||
if(c<0) return -1;
|
||||
else if(c>0) return 1;
|
||||
else return ArrayComparator.comparator.compare(parameter,v.parameter);
|
||||
}
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer buffer=new StringBuffer();
|
||||
buffer.append(name);
|
||||
buffer.append("(");
|
||||
for(int i=0;i<parameter.length;i++) {
|
||||
buffer.append(parameter[i]).append(i<parameter.length-1?", ":"");
|
||||
}
|
||||
buffer.append(")");
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
public String toJava() {
|
||||
StringBuffer buffer=new StringBuffer();
|
||||
buffer.append(parameter[0].toJava());
|
||||
buffer.append(".").append(name).append("()");
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
public void toMathML(MathML element, Object data) {
|
||||
MathML e1;
|
||||
int exponent=data instanceof Integer?((Integer)data).intValue():1;
|
||||
if(exponent==1) nameToMathML(element);
|
||||
else {
|
||||
e1=element.element("msup");
|
||||
nameToMathML(e1);
|
||||
MathML e2=element.element("mn");
|
||||
e2.appendChild(element.text(String.valueOf(exponent)));
|
||||
e1.appendChild(e2);
|
||||
element.appendChild(e1);
|
||||
}
|
||||
e1=element.element("mfenced");
|
||||
for(int i=0;i<parameter.length;i++) {
|
||||
parameter[i].toMathML(e1,null);
|
||||
}
|
||||
element.appendChild(e1);
|
||||
}
|
||||
}
|
223
src/misc/src/jscl/math/function/ImplicitFunction.java
Normal file
223
src/misc/src/jscl/math/function/ImplicitFunction.java
Normal file
@ -0,0 +1,223 @@
|
||||
package jscl.math.function;
|
||||
|
||||
import jscl.math.Generic;
|
||||
import jscl.math.NotIntegrableException;
|
||||
import jscl.math.Variable;
|
||||
import jscl.mathml.MathML;
|
||||
import jscl.util.ArrayComparator;
|
||||
|
||||
public class ImplicitFunction extends Function {
|
||||
protected int derivation[];
|
||||
protected Generic subscript[];
|
||||
|
||||
public ImplicitFunction(String name, Generic parameter[], int derivation[], Generic subscript[]) {
|
||||
super(name,parameter);
|
||||
this.derivation=derivation;
|
||||
this.subscript=subscript;
|
||||
}
|
||||
|
||||
public int[] derivation() {
|
||||
return derivation;
|
||||
}
|
||||
|
||||
public Generic[] subscript() {
|
||||
return subscript;
|
||||
}
|
||||
|
||||
public Generic antiderivative(int n) throws NotIntegrableException {
|
||||
int c[]=new int[derivation.length];
|
||||
for(int i=0;i<c.length;i++) {
|
||||
if(i==n) {
|
||||
if(derivation[i]>0) c[i]=derivation[i]-1;
|
||||
else throw new NotIntegrableException();
|
||||
} else c[i]=derivation[i];
|
||||
}
|
||||
return new ImplicitFunction(name,parameter,c,subscript).evaluate();
|
||||
}
|
||||
|
||||
public Generic derivative(int n) {
|
||||
int c[]=new int[derivation.length];
|
||||
for(int i=0;i<c.length;i++) {
|
||||
if(i==n) c[i]=derivation[i]+1;
|
||||
else c[i]=derivation[i];
|
||||
}
|
||||
return new ImplicitFunction(name,parameter,c,subscript).evaluate();
|
||||
}
|
||||
|
||||
public Generic evaluate() {
|
||||
return expressionValue();
|
||||
}
|
||||
|
||||
public Generic evalelem() {
|
||||
return expressionValue();
|
||||
}
|
||||
|
||||
public Generic evalsimp() {
|
||||
return expressionValue();
|
||||
}
|
||||
|
||||
public Generic evalnum() {
|
||||
throw new ArithmeticException();
|
||||
}
|
||||
|
||||
public int compareTo(Variable variable) {
|
||||
if(this==variable) return 0;
|
||||
int c=comparator.compare(this,variable);
|
||||
if(c<0) return -1;
|
||||
else if(c>0) return 1;
|
||||
else {
|
||||
ImplicitFunction v=(ImplicitFunction)variable;
|
||||
c=name.compareTo(v.name);
|
||||
if(c<0) return -1;
|
||||
else if(c>0) return 1;
|
||||
else {
|
||||
c=ArrayComparator.comparator.compare(subscript,v.subscript);
|
||||
if(c<0) return -1;
|
||||
else if(c>0) return 1;
|
||||
else {
|
||||
c=compareDerivation(derivation,v.derivation);
|
||||
if(c<0) return -1;
|
||||
else if(c>0) return 1;
|
||||
else return ArrayComparator.comparator.compare(parameter,v.parameter);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static int compareDerivation(int c1[], int c2[]) {
|
||||
int n=c1.length;
|
||||
for(int i=n-1;i>=0;i--) {
|
||||
if(c1[i]<c2[i]) return -1;
|
||||
else if(c1[i]>c2[i]) return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer buffer=new StringBuffer();
|
||||
int n=0;
|
||||
for(int i=0;i<derivation.length;i++) n+=derivation[i];
|
||||
buffer.append(name);
|
||||
for(int i=0;i<subscript.length;i++) {
|
||||
buffer.append("[").append(subscript[i]).append("]");
|
||||
}
|
||||
if(n==0);
|
||||
else if(parameter.length==1?n<=Constant.PRIMECHARS:false) buffer.append(Constant.primechars(n));
|
||||
else buffer.append(derivationToString());
|
||||
buffer.append("(");
|
||||
for(int i=0;i<parameter.length;i++) {
|
||||
buffer.append(parameter[i]).append(i<parameter.length-1?", ":"");
|
||||
}
|
||||
buffer.append(")");
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
String derivationToString() {
|
||||
StringBuffer buffer=new StringBuffer();
|
||||
buffer.append("{");
|
||||
for(int i=0;i<derivation.length;i++) {
|
||||
buffer.append(derivation[i]).append(i<derivation.length-1?", ":"");
|
||||
}
|
||||
buffer.append("}");
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
public String toJava() {
|
||||
StringBuffer buffer=new StringBuffer();
|
||||
int n=0;
|
||||
for(int i=0;i<derivation.length;i++) n+=derivation[i];
|
||||
buffer.append(name);
|
||||
if(n==0);
|
||||
else if(parameter.length==1?n<=Constant.PRIMECHARS:false) buffer.append(Constant.underscores(n));
|
||||
else buffer.append(derivationToJava());
|
||||
buffer.append("(");
|
||||
for(int i=0;i<parameter.length;i++) {
|
||||
buffer.append(parameter[i].toJava()).append(i<parameter.length-1?", ":"");
|
||||
}
|
||||
buffer.append(")");
|
||||
for(int i=0;i<subscript.length;i++) {
|
||||
buffer.append("[").append(subscript[i].integerValue().intValue()).append("]");
|
||||
}
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
String derivationToJava() {
|
||||
StringBuffer buffer=new StringBuffer();
|
||||
for(int i=0;i<derivation.length;i++) {
|
||||
buffer.append("_").append(derivation[i]);
|
||||
}
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
public void toMathML(MathML element, Object data) {
|
||||
MathML e1;
|
||||
int exponent=data instanceof Integer?((Integer)data).intValue():1;
|
||||
if(exponent==1) bodyToMathML(element);
|
||||
else {
|
||||
e1=element.element("msup");
|
||||
bodyToMathML(e1);
|
||||
MathML e2=element.element("mn");
|
||||
e2.appendChild(element.text(String.valueOf(exponent)));
|
||||
e1.appendChild(e2);
|
||||
element.appendChild(e1);
|
||||
}
|
||||
e1=element.element("mfenced");
|
||||
for(int i=0;i<parameter.length;i++) {
|
||||
parameter[i].toMathML(e1,null);
|
||||
}
|
||||
element.appendChild(e1);
|
||||
}
|
||||
|
||||
void bodyToMathML(MathML element) {
|
||||
int n=0;
|
||||
for(int i=0;i<derivation.length;i++) n+=derivation[i];
|
||||
if(subscript.length==0) {
|
||||
if(n==0) {
|
||||
nameToMathML(element);
|
||||
} else {
|
||||
MathML e1=element.element("msup");
|
||||
nameToMathML(e1);
|
||||
derivationToMathML(e1,n);
|
||||
element.appendChild(e1);
|
||||
}
|
||||
} else {
|
||||
if(n==0) {
|
||||
MathML e1=element.element("msub");
|
||||
nameToMathML(e1);
|
||||
MathML e2=element.element("mrow");
|
||||
for(int i=0;i<subscript.length;i++) {
|
||||
subscript[i].toMathML(e2,null);
|
||||
}
|
||||
e1.appendChild(e2);
|
||||
element.appendChild(e1);
|
||||
} else {
|
||||
MathML e1=element.element("msubsup");
|
||||
nameToMathML(e1);
|
||||
MathML e2=element.element("mrow");
|
||||
for(int i=0;i<subscript.length;i++) {
|
||||
subscript[i].toMathML(e2,null);
|
||||
}
|
||||
e1.appendChild(e2);
|
||||
derivationToMathML(e1,n);
|
||||
element.appendChild(e1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void derivationToMathML(MathML element, int n) {
|
||||
if(parameter.length==1?n<=Constant.PRIMECHARS:false) Constant.primecharsToMathML(element,n);
|
||||
else {
|
||||
MathML e1=element.element("mfenced");
|
||||
for(int i=0;i<derivation.length;i++) {
|
||||
MathML e2=element.element("mn");
|
||||
e2.appendChild(element.text(String.valueOf(derivation[i])));
|
||||
e1.appendChild(e2);
|
||||
}
|
||||
element.appendChild(e1);
|
||||
}
|
||||
}
|
||||
|
||||
protected Variable newinstance() {
|
||||
return new ImplicitFunction(name,new Generic[parameter.length],derivation,subscript);
|
||||
}
|
||||
}
|
27
src/misc/src/jscl/math/function/Inv.java
Normal file
27
src/misc/src/jscl/math/function/Inv.java
Normal file
@ -0,0 +1,27 @@
|
||||
package jscl.math.function;
|
||||
|
||||
import jscl.math.Generic;
|
||||
import jscl.math.JSCLInteger;
|
||||
import jscl.math.NotDivisibleException;
|
||||
import jscl.math.Variable;
|
||||
|
||||
public class Inv extends Frac {
|
||||
public Inv(Generic generic) {
|
||||
super(JSCLInteger.valueOf(1),generic);
|
||||
}
|
||||
|
||||
public Generic evaluate() {
|
||||
try {
|
||||
return JSCLInteger.valueOf(1).divide(parameter());
|
||||
} catch (NotDivisibleException e) {}
|
||||
return expressionValue();
|
||||
}
|
||||
|
||||
public Generic parameter() {
|
||||
return parameter[1];
|
||||
}
|
||||
|
||||
protected Variable newinstance() {
|
||||
return new Inv(null);
|
||||
}
|
||||
}
|
75
src/misc/src/jscl/math/function/Log.java
Normal file
75
src/misc/src/jscl/math/function/Log.java
Normal file
@ -0,0 +1,75 @@
|
||||
package jscl.math.function;
|
||||
|
||||
import jscl.math.Generic;
|
||||
import jscl.math.JSCLInteger;
|
||||
import jscl.math.NotIntegerException;
|
||||
import jscl.math.NotIntegrableException;
|
||||
import jscl.math.NotVariableException;
|
||||
import jscl.math.NumericWrapper;
|
||||
import jscl.math.Power;
|
||||
import jscl.math.Variable;
|
||||
|
||||
public class Log extends Function {
|
||||
public Log(Generic generic) {
|
||||
super("log",new Generic[] {generic});
|
||||
}
|
||||
|
||||
public Generic antiderivative(int n) throws NotIntegrableException {
|
||||
throw new NotIntegrableException();
|
||||
}
|
||||
|
||||
public Generic derivative(int n) {
|
||||
return new Inv(parameter[0]).evaluate();
|
||||
}
|
||||
|
||||
public Generic evaluate() {
|
||||
if(parameter[0].compareTo(JSCLInteger.valueOf(1))==0) {
|
||||
return JSCLInteger.valueOf(0);
|
||||
}
|
||||
return expressionValue();
|
||||
}
|
||||
|
||||
public Generic evalelem() {
|
||||
return evaluate();
|
||||
}
|
||||
|
||||
public Generic evalsimp() {
|
||||
try {
|
||||
JSCLInteger en=parameter[0].integerValue();
|
||||
if(en.signum()<0) return Constant.i.multiply(Constant.pi).add(new Log(en.negate()).evalsimp());
|
||||
else {
|
||||
Generic a=en.factorize();
|
||||
Generic p[]=a.productValue();
|
||||
Generic s=JSCLInteger.valueOf(0);
|
||||
for(int i=0;i<p.length;i++) {
|
||||
Power o=p[i].powerValue();
|
||||
s=s.add(JSCLInteger.valueOf(o.exponent()).multiply(new Log(o.value(true)).expressionValue()));
|
||||
}
|
||||
return s;
|
||||
}
|
||||
} catch (NotIntegerException e) {}
|
||||
try {
|
||||
Variable v=parameter[0].variableValue();
|
||||
if(v instanceof Sqrt) {
|
||||
Generic g[]=((Sqrt)v).parameters();
|
||||
return Constant.half.multiply(new Log(g[0]).evalsimp());
|
||||
}
|
||||
} catch (NotVariableException e) {}
|
||||
Generic n[]=Frac.separateCoefficient(parameter[0]);
|
||||
if(n[0].compareTo(JSCLInteger.valueOf(1))==0 && n[1].compareTo(JSCLInteger.valueOf(1))==0);
|
||||
else return new Log(n[2]).evalsimp().add(
|
||||
new Log(n[0]).evalsimp()
|
||||
).subtract(
|
||||
new Log(n[1]).evalsimp()
|
||||
);
|
||||
return expressionValue();
|
||||
}
|
||||
|
||||
public Generic evalnum() {
|
||||
return ((NumericWrapper)parameter[0]).log();
|
||||
}
|
||||
|
||||
protected Variable newinstance() {
|
||||
return new Log(null);
|
||||
}
|
||||
}
|
9
src/misc/src/jscl/math/function/NotRootException.java
Normal file
9
src/misc/src/jscl/math/function/NotRootException.java
Normal file
@ -0,0 +1,9 @@
|
||||
package jscl.math.function;
|
||||
|
||||
public class NotRootException extends ArithmeticException {
|
||||
public NotRootException() {}
|
||||
|
||||
public NotRootException(String s) {
|
||||
super(s);
|
||||
}
|
||||
}
|
264
src/misc/src/jscl/math/function/Pow.java
Normal file
264
src/misc/src/jscl/math/function/Pow.java
Normal file
@ -0,0 +1,264 @@
|
||||
package jscl.math.function;
|
||||
|
||||
import jscl.math.Antiderivative;
|
||||
import jscl.math.Generic;
|
||||
import jscl.math.GenericVariable;
|
||||
import jscl.math.JSCLInteger;
|
||||
import jscl.math.NotIntegerException;
|
||||
import jscl.math.NotIntegrableException;
|
||||
import jscl.math.NotPowerException;
|
||||
import jscl.math.NotVariableException;
|
||||
import jscl.math.NumericWrapper;
|
||||
import jscl.math.Power;
|
||||
import jscl.math.Variable;
|
||||
import jscl.mathml.MathML;
|
||||
|
||||
public class Pow extends Algebraic {
|
||||
public Pow(Generic generic, Generic exponent) {
|
||||
super("pow",new Generic[] {generic,exponent});
|
||||
}
|
||||
|
||||
public Root rootValue() throws NotRootException {
|
||||
try {
|
||||
Variable v=parameter[1].variableValue();
|
||||
if(v instanceof Inv) {
|
||||
Generic g=((Inv)v).parameter();
|
||||
try {
|
||||
int d=g.integerValue().intValue();
|
||||
if(d>0) {
|
||||
Generic a[]=new Generic[d+1];
|
||||
a[0]=parameter[0].negate();
|
||||
for(int i=1;i<d;i++) a[i]=JSCLInteger.valueOf(0);
|
||||
a[d]=JSCLInteger.valueOf(1);
|
||||
return new Root(a,0);
|
||||
}
|
||||
} catch (NotIntegerException e) {}
|
||||
}
|
||||
} catch (NotVariableException e) {}
|
||||
throw new NotRootException();
|
||||
}
|
||||
|
||||
public Generic antiderivative(Variable variable) throws NotIntegrableException {
|
||||
try {
|
||||
Root r=rootValue();
|
||||
Generic g[]=r.parameters();
|
||||
if(g[0].isPolynomial(variable)) {
|
||||
return Antiderivative.compute(r,variable);
|
||||
} else throw new NotIntegrableException();
|
||||
} catch (NotRootException e) {}
|
||||
return super.antiderivative(variable);
|
||||
}
|
||||
|
||||
public Generic antiderivative(int n) throws NotIntegrableException {
|
||||
if(n==0) {
|
||||
return new Pow(parameter[0],parameter[1].add(JSCLInteger.valueOf(1))).evaluate().multiply(new Inv(parameter[1].add(JSCLInteger.valueOf(1))).evaluate());
|
||||
} else {
|
||||
return new Pow(parameter[0],parameter[1]).evaluate().multiply(new Inv(new Log(parameter[0]).evaluate()).evaluate());
|
||||
}
|
||||
}
|
||||
|
||||
public Generic derivative(int n) {
|
||||
if(n==0) {
|
||||
return new Pow(parameter[0],parameter[1].subtract(JSCLInteger.valueOf(1))).evaluate().multiply(parameter[1]);
|
||||
} else {
|
||||
return new Pow(parameter[0],parameter[1]).evaluate().multiply(new Log(parameter[0]).evaluate());
|
||||
}
|
||||
}
|
||||
|
||||
public Generic evaluate() {
|
||||
if(parameter[0].compareTo(JSCLInteger.valueOf(1))==0) {
|
||||
return JSCLInteger.valueOf(1);
|
||||
}
|
||||
if(parameter[1].signum()<0) {
|
||||
return new Pow(new Inv(parameter[0]).evaluate(),parameter[1].negate()).evaluate();
|
||||
}
|
||||
try {
|
||||
int c=parameter[1].integerValue().intValue();
|
||||
return parameter[0].pow(c);
|
||||
} catch (NotIntegerException e) {}
|
||||
try {
|
||||
Root r=rootValue();
|
||||
int d=r.degree();
|
||||
Generic g[]=r.parameters();
|
||||
Generic a=g[0].negate();
|
||||
try {
|
||||
JSCLInteger en=a.integerValue();
|
||||
if(en.signum()<0);
|
||||
else {
|
||||
Generic rt=en.nthrt(d);
|
||||
if(rt.pow(d).compareTo(en)==0) return rt;
|
||||
}
|
||||
} catch (NotIntegerException e) {}
|
||||
} catch (NotRootException e) {}
|
||||
return expressionValue();
|
||||
}
|
||||
|
||||
public Generic evalelem() {
|
||||
return new Exp(
|
||||
new Log(
|
||||
parameter[0]
|
||||
).evalelem().multiply(
|
||||
parameter[1]
|
||||
)
|
||||
).evalelem();
|
||||
}
|
||||
|
||||
public Generic evalsimp() {
|
||||
if(parameter[0].compareTo(JSCLInteger.valueOf(1))==0) {
|
||||
return JSCLInteger.valueOf(1);
|
||||
}
|
||||
if(parameter[1].signum()<0) {
|
||||
return new Pow(new Inv(parameter[0]).evalsimp(),parameter[1].negate()).evalsimp();
|
||||
}
|
||||
try {
|
||||
int c=parameter[1].integerValue().intValue();
|
||||
return parameter[0].pow(c);
|
||||
} catch (NotIntegerException e) {}
|
||||
try {
|
||||
Root r=rootValue();
|
||||
int d=r.degree();
|
||||
Generic g[]=r.parameters();
|
||||
Generic a=g[0].negate();
|
||||
try {
|
||||
JSCLInteger en=a.integerValue();
|
||||
if(en.signum()<0);
|
||||
else {
|
||||
Generic rt=en.nthrt(d);
|
||||
if(rt.pow(d).compareTo(en)==0) return rt;
|
||||
}
|
||||
} catch (NotIntegerException e) {}
|
||||
switch(d) {
|
||||
case 2:
|
||||
return new Sqrt(a).evalsimp();
|
||||
case 3:
|
||||
case 4:
|
||||
case 6:
|
||||
if(a.compareTo(JSCLInteger.valueOf(-1))==0) return root_minus_1(d);
|
||||
}
|
||||
} catch (NotRootException e) {
|
||||
Generic n[]=Frac.separateCoefficient(parameter[1]);
|
||||
if(n[0].compareTo(JSCLInteger.valueOf(1))==0 && n[1].compareTo(JSCLInteger.valueOf(1))==0);
|
||||
else return new Pow(
|
||||
new Pow(
|
||||
new Pow(
|
||||
parameter[0],
|
||||
n[2]
|
||||
).evalsimp(),
|
||||
new Inv(
|
||||
n[1]
|
||||
).evalsimp()
|
||||
).evalsimp(),
|
||||
n[0]
|
||||
).evalsimp();
|
||||
}
|
||||
return expressionValue();
|
||||
}
|
||||
|
||||
static Generic root_minus_1(int d) {
|
||||
switch(d) {
|
||||
case 1:
|
||||
return JSCLInteger.valueOf(-1);
|
||||
case 2:
|
||||
return Constant.i;
|
||||
case 3:
|
||||
return Constant.jbar.negate();
|
||||
case 4:
|
||||
return new Sqrt(Constant.half).expressionValue().multiply(JSCLInteger.valueOf(1).add(Constant.i));
|
||||
case 6:
|
||||
return Constant.half.multiply(new Sqrt(JSCLInteger.valueOf(3)).expressionValue().add(Constant.i));
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public Generic evalnum() {
|
||||
return ((NumericWrapper)parameter[0]).pow((NumericWrapper)parameter[1]);
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer buffer=new StringBuffer();
|
||||
try {
|
||||
JSCLInteger en=parameter[0].integerValue();
|
||||
if(en.signum()<0) buffer.append(GenericVariable.valueOf(en,true));
|
||||
else buffer.append(en);
|
||||
} catch (NotIntegerException e) {
|
||||
try {
|
||||
Variable v=parameter[0].variableValue();
|
||||
if(v instanceof Frac || v instanceof Pow) {
|
||||
buffer.append(GenericVariable.valueOf(parameter[0]));
|
||||
} else buffer.append(v);
|
||||
} catch (NotVariableException e2) {
|
||||
try {
|
||||
Power o=parameter[0].powerValue();
|
||||
if(o.exponent()==1) buffer.append(o.value(true));
|
||||
else buffer.append(GenericVariable.valueOf(parameter[0]));
|
||||
} catch (NotPowerException e3) {
|
||||
buffer.append(GenericVariable.valueOf(parameter[0]));
|
||||
}
|
||||
}
|
||||
}
|
||||
buffer.append("^");
|
||||
try {
|
||||
JSCLInteger en=parameter[1].integerValue();
|
||||
buffer.append(en);
|
||||
} catch (NotIntegerException e) {
|
||||
try {
|
||||
Variable v=parameter[1].variableValue();
|
||||
if(v instanceof Frac) {
|
||||
buffer.append(GenericVariable.valueOf(parameter[1]));
|
||||
} else buffer.append(v);
|
||||
} catch (NotVariableException e2) {
|
||||
try {
|
||||
parameter[1].powerValue();
|
||||
buffer.append(parameter[1]);
|
||||
} catch (NotPowerException e3) {
|
||||
buffer.append(GenericVariable.valueOf(parameter[1]));
|
||||
}
|
||||
}
|
||||
}
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
public String toJava() {
|
||||
StringBuffer buffer=new StringBuffer();
|
||||
buffer.append(parameter[0].toJava());
|
||||
buffer.append(".pow(");
|
||||
buffer.append(parameter[1].toJava());
|
||||
buffer.append(")");
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
void bodyToMathML(MathML element, boolean fenced) {
|
||||
if(fenced) {
|
||||
MathML e1=element.element("mfenced");
|
||||
bodyToMathML(e1);
|
||||
element.appendChild(e1);
|
||||
} else {
|
||||
bodyToMathML(element);
|
||||
}
|
||||
}
|
||||
|
||||
void bodyToMathML(MathML element) {
|
||||
MathML e1=element.element("msup");
|
||||
try {
|
||||
Variable v=parameter[0].variableValue();
|
||||
if(v instanceof Frac || v instanceof Pow || v instanceof Exp) {
|
||||
GenericVariable.valueOf(parameter[0]).toMathML(e1,null);
|
||||
} else parameter[0].toMathML(e1,null);
|
||||
} catch (NotVariableException e2) {
|
||||
try {
|
||||
Power o=parameter[0].powerValue();
|
||||
if(o.exponent()==1) o.value(true).toMathML(e1,null);
|
||||
else GenericVariable.valueOf(parameter[0]).toMathML(e1,null);
|
||||
} catch (NotPowerException e3) {
|
||||
GenericVariable.valueOf(parameter[0]).toMathML(e1,null);
|
||||
}
|
||||
}
|
||||
parameter[1].toMathML(e1,null);
|
||||
element.appendChild(e1);
|
||||
}
|
||||
|
||||
protected Variable newinstance() {
|
||||
return new Pow(null,null);
|
||||
}
|
||||
}
|
404
src/misc/src/jscl/math/function/Root.java
Normal file
404
src/misc/src/jscl/math/function/Root.java
Normal file
@ -0,0 +1,404 @@
|
||||
package jscl.math.function;
|
||||
|
||||
import jscl.math.Antiderivative;
|
||||
import jscl.math.Generic;
|
||||
import jscl.math.JSCLInteger;
|
||||
import jscl.math.NotIntegerException;
|
||||
import jscl.math.NotIntegrableException;
|
||||
import jscl.math.NumericWrapper;
|
||||
import jscl.math.TechnicalVariable;
|
||||
import jscl.math.Variable;
|
||||
import jscl.math.polynomial.Polynomial;
|
||||
import jscl.math.polynomial.UnivariatePolynomial;
|
||||
import jscl.mathml.MathML;
|
||||
import jscl.util.ArrayComparator;
|
||||
|
||||
public class Root extends Algebraic {
|
||||
protected Generic subscript;
|
||||
|
||||
public Root(Generic parameter[], Generic subscript) {
|
||||
super("root",parameter);
|
||||
this.subscript=subscript;
|
||||
}
|
||||
|
||||
public Root(Generic parameter[], int s) {
|
||||
this(parameter, JSCLInteger.valueOf(s));
|
||||
}
|
||||
|
||||
public Root(UnivariatePolynomial polynomial, int s) {
|
||||
this(polynomial.normalize().elements(),s);
|
||||
}
|
||||
|
||||
public Generic subscript() {
|
||||
return subscript;
|
||||
}
|
||||
|
||||
public Root rootValue() {
|
||||
return this;
|
||||
}
|
||||
|
||||
public Generic antiderivative(Variable variable) throws NotIntegrableException {
|
||||
boolean b=true;
|
||||
for(int i=0;i<parameter.length;i++) b=b && parameter[i].isPolynomial(variable);
|
||||
if(b) {
|
||||
return Antiderivative.compute(this,variable);
|
||||
} else throw new NotIntegrableException();
|
||||
}
|
||||
|
||||
public Generic derivative(Variable variable) {
|
||||
if(compareTo(variable)==0) return JSCLInteger.valueOf(1);
|
||||
else {
|
||||
Variable t=new TechnicalVariable("t");
|
||||
Generic a[]=new Generic[parameter.length];
|
||||
for(int i=0;i<parameter.length;i++) a[i]=parameter[i].derivative(variable);
|
||||
UnivariatePolynomial fact=(UnivariatePolynomial)Polynomial.factory(this);
|
||||
UnivariatePolynomial p=fact.valueof(parameter);
|
||||
UnivariatePolynomial q=(UnivariatePolynomial)p.derivative().multiply(t.expressionValue()).add(fact.valueof(a));
|
||||
UnivariatePolynomial r=(UnivariatePolynomial)Polynomial.factory(t).valueof(p.resultant(q));
|
||||
return new Root(r.elements(),subscript).evaluate();
|
||||
}
|
||||
}
|
||||
|
||||
public Generic derivative(int n) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public Generic substitute(Variable variable, Generic generic) {
|
||||
Root v=(Root)newinstance();
|
||||
for(int i=0;i<parameter.length;i++) {
|
||||
v.parameter[i]=parameter[i].substitute(variable,generic);
|
||||
}
|
||||
v.subscript=subscript.substitute(variable,generic);
|
||||
if(v.isIdentity(variable)) return generic;
|
||||
else return v.evaluate();
|
||||
}
|
||||
|
||||
public Generic expand() {
|
||||
Root v=(Root)newinstance();
|
||||
for(int i=0;i<parameter.length;i++) {
|
||||
v.parameter[i]=parameter[i].expand();
|
||||
}
|
||||
v.subscript=subscript.expand();
|
||||
return v.evaluate();
|
||||
}
|
||||
|
||||
public Generic factorize() {
|
||||
Root v=(Root)newinstance();
|
||||
for(int i=0;i<parameter.length;i++) {
|
||||
v.parameter[i]=parameter[i].factorize();
|
||||
}
|
||||
v.subscript=subscript;
|
||||
return v.expressionValue();
|
||||
}
|
||||
|
||||
public Generic elementary() {
|
||||
Root v=(Root)newinstance();
|
||||
for(int i=0;i<parameter.length;i++) {
|
||||
v.parameter[i]=parameter[i].elementary();
|
||||
}
|
||||
v.subscript=subscript.elementary();
|
||||
return v.evalelem();
|
||||
}
|
||||
|
||||
public Generic simplify() {
|
||||
Root v=(Root)newinstance();
|
||||
for(int i=0;i<parameter.length;i++) {
|
||||
v.parameter[i]=parameter[i].simplify();
|
||||
}
|
||||
v.subscript=subscript.simplify();
|
||||
return v.evalsimp();
|
||||
}
|
||||
|
||||
public Generic numeric() {
|
||||
Root v=(Root)newinstance();
|
||||
for(int i=0;i<parameter.length;i++) {
|
||||
v.parameter[i]=parameter[i].numeric();
|
||||
}
|
||||
v.subscript=subscript;
|
||||
return v.evalnum();
|
||||
}
|
||||
|
||||
public Generic evaluate() {
|
||||
if(isZero()) return JSCLInteger.valueOf(0);
|
||||
try {
|
||||
int s=subscript.integerValue().intValue();
|
||||
switch(degree()) {
|
||||
case 1:
|
||||
return new Frac(parameter[0],parameter[1]).evaluate().negate();
|
||||
}
|
||||
} catch (NotIntegerException e) {}
|
||||
return expressionValue();
|
||||
}
|
||||
|
||||
public Generic evalelem() {
|
||||
return evaluate();
|
||||
}
|
||||
|
||||
public Generic evalsimp() {
|
||||
if(isZero()) return JSCLInteger.valueOf(0);
|
||||
try {
|
||||
int s=subscript.integerValue().intValue();
|
||||
switch(degree()) {
|
||||
case 1:
|
||||
return linear(parameter);
|
||||
case 2:
|
||||
return quadratic(parameter,s);
|
||||
case 3:
|
||||
return cubic(parameter,s);
|
||||
case 4:
|
||||
return quartic(parameter,s);
|
||||
default:
|
||||
if(isNth() && s==0) return nth(parameter);
|
||||
}
|
||||
} catch (NotIntegerException e) {}
|
||||
return expressionValue();
|
||||
}
|
||||
|
||||
boolean isZero() {
|
||||
boolean b=degree()>0;
|
||||
for(int i=0;i<degree();i++) b=b && parameter[i].signum()==0;
|
||||
b=b && parameter[degree()].signum()!=0;
|
||||
return b;
|
||||
}
|
||||
|
||||
boolean isNth() {
|
||||
boolean b=degree()>0;
|
||||
for(int i=1;i<degree();i++) b=b && parameter[i].signum()==0;
|
||||
b=b && parameter[degree()].signum()!=0;
|
||||
return b;
|
||||
}
|
||||
|
||||
static Generic nth(Generic parameter[]) {
|
||||
int degree=parameter.length-1;
|
||||
Generic a=new Frac(parameter[0],parameter[degree]).evalsimp();
|
||||
return new Pow(
|
||||
a.negate(),
|
||||
new Inv(JSCLInteger.valueOf(degree)).evalsimp()
|
||||
).evalsimp();
|
||||
}
|
||||
|
||||
static Generic linear(Generic parameter[]) {
|
||||
Generic a=new Frac(parameter[0],parameter[1]).evalsimp();
|
||||
return a.negate();
|
||||
}
|
||||
|
||||
static Generic quadratic(Generic parameter[], int subscript) {
|
||||
Generic a=new Frac(parameter[1],parameter[2]).evalsimp();
|
||||
Generic b=new Frac(parameter[0],parameter[2]).evalsimp();
|
||||
Generic y=new Sqrt(
|
||||
a.pow(2).subtract(JSCLInteger.valueOf(4).multiply(b))
|
||||
).evalsimp();
|
||||
switch(subscript) {
|
||||
case 0:
|
||||
return new Frac(
|
||||
a.subtract(y),
|
||||
JSCLInteger.valueOf(2)
|
||||
).evalsimp().negate();
|
||||
default:
|
||||
return new Frac(
|
||||
a.add(y),
|
||||
JSCLInteger.valueOf(2)
|
||||
).evalsimp().negate();
|
||||
}
|
||||
}
|
||||
|
||||
static Generic cubic(Generic parameter[], int subscript) {
|
||||
Generic a=new Frac(parameter[2],parameter[3]).evalsimp();
|
||||
Generic b=new Frac(parameter[1],parameter[3]).evalsimp();
|
||||
Generic c=new Frac(parameter[0],parameter[3]).evalsimp();
|
||||
Generic y[]=new Generic[2];
|
||||
for(int i=0;i<y.length;i++) {
|
||||
y[i]=new Cubic(
|
||||
new Root(
|
||||
new Generic[] {
|
||||
a.pow(6).subtract(JSCLInteger.valueOf(9).multiply(a.pow(4)).multiply(b)).add(JSCLInteger.valueOf(27).multiply(a.pow(2)).multiply(b.pow(2))).subtract(JSCLInteger.valueOf(27).multiply(b.pow(3))),
|
||||
JSCLInteger.valueOf(2).multiply(a.pow(3)).subtract(JSCLInteger.valueOf(9).multiply(a).multiply(b)).add(JSCLInteger.valueOf(27).multiply(c)),
|
||||
JSCLInteger.valueOf(1)
|
||||
},
|
||||
i
|
||||
).evalsimp()
|
||||
).evalsimp();
|
||||
}
|
||||
switch(subscript) {
|
||||
case 0:
|
||||
return new Frac(
|
||||
a.subtract(y[0]).subtract(y[1]),
|
||||
JSCLInteger.valueOf(3)
|
||||
).evalsimp().negate();
|
||||
case 1:
|
||||
return new Frac(
|
||||
a.subtract(Constant.j.multiply(y[0])).subtract(Constant.jbar.multiply(y[1])),
|
||||
JSCLInteger.valueOf(3)
|
||||
).evalsimp().negate();
|
||||
default:
|
||||
return new Frac(
|
||||
a.subtract(Constant.jbar.multiply(y[0])).subtract(Constant.j.multiply(y[1])),
|
||||
JSCLInteger.valueOf(3)
|
||||
).evalsimp().negate();
|
||||
}
|
||||
}
|
||||
|
||||
static Generic quartic(Generic parameter[], int subscript) {
|
||||
Generic a=new Frac(parameter[3],parameter[4]).evalsimp();
|
||||
Generic b=new Frac(parameter[2],parameter[4]).evalsimp();
|
||||
Generic c=new Frac(parameter[1],parameter[4]).evalsimp();
|
||||
Generic d=new Frac(parameter[0],parameter[4]).evalsimp();
|
||||
Generic y[]=new Generic[3];
|
||||
for(int i=0;i<y.length;i++) {
|
||||
y[i]=new Sqrt(
|
||||
new Root(
|
||||
new Generic[] {
|
||||
a.pow(6).subtract(JSCLInteger.valueOf(8).multiply(a.pow(4)).multiply(b)).add(JSCLInteger.valueOf(16).multiply(a.pow(2)).multiply(b.pow(2))).add(JSCLInteger.valueOf(16).multiply(a.pow(3)).multiply(c)).subtract(JSCLInteger.valueOf(64).multiply(a).multiply(b).multiply(c)).add(JSCLInteger.valueOf(64).multiply(c.pow(2))),
|
||||
JSCLInteger.valueOf(-3).multiply(a.pow(4)).add(JSCLInteger.valueOf(16).multiply(a.pow(2)).multiply(b)).subtract(JSCLInteger.valueOf(16).multiply(b.pow(2))).subtract(JSCLInteger.valueOf(16).multiply(a).multiply(c)).add(JSCLInteger.valueOf(64).multiply(d)),
|
||||
JSCLInteger.valueOf(3).multiply(a.pow(2)).subtract(JSCLInteger.valueOf(8).multiply(b)),
|
||||
JSCLInteger.valueOf(-1)
|
||||
},
|
||||
i
|
||||
).evalsimp()
|
||||
).evalsimp();
|
||||
}
|
||||
switch(subscript) {
|
||||
case 0:
|
||||
return new Frac(
|
||||
a.add(y[0]).subtract(y[1]).subtract(y[2]),
|
||||
JSCLInteger.valueOf(4)
|
||||
).evalsimp().negate();
|
||||
case 1:
|
||||
return new Frac(
|
||||
a.subtract(y[0]).subtract(y[1]).add(y[2]),
|
||||
JSCLInteger.valueOf(4)
|
||||
).evalsimp().negate();
|
||||
case 2:
|
||||
return new Frac(
|
||||
a.add(y[0]).add(y[1]).add(y[2]),
|
||||
JSCLInteger.valueOf(4)
|
||||
).evalsimp().negate();
|
||||
default:
|
||||
return new Frac(
|
||||
a.subtract(y[0]).add(y[1]).subtract(y[2]),
|
||||
JSCLInteger.valueOf(4)
|
||||
).evalsimp().negate();
|
||||
}
|
||||
}
|
||||
|
||||
public int degree() {
|
||||
return parameter.length-1;
|
||||
}
|
||||
|
||||
public Generic evalnum() {
|
||||
return NumericWrapper.root(subscript.integerValue().intValue(),parameter);
|
||||
}
|
||||
|
||||
public int compareTo(Variable variable) {
|
||||
if(this==variable) return 0;
|
||||
int c=comparator.compare(this,variable);
|
||||
if(c<0) return -1;
|
||||
else if(c>0) return 1;
|
||||
else {
|
||||
Root v=(Root)variable;
|
||||
c=ArrayComparator.comparator.compare(parameter,v.parameter);
|
||||
if(c<0) return -1;
|
||||
else if(c>0) return 1;
|
||||
else return subscript.compareTo(v.subscript);
|
||||
}
|
||||
}
|
||||
|
||||
public static Generic sigma(Generic parameter[], int n) {
|
||||
Sigma s=new Sigma(parameter,n);
|
||||
s.compute();
|
||||
return s.getValue();
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer buffer=new StringBuffer();
|
||||
buffer.append(name);
|
||||
buffer.append("[").append(subscript).append("]");
|
||||
buffer.append("(");
|
||||
for(int i=0;i<parameter.length;i++) {
|
||||
buffer.append(parameter[i]).append(i<parameter.length-1?", ":"");
|
||||
}
|
||||
buffer.append(")");
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
public String toJava() {
|
||||
StringBuffer buffer=new StringBuffer();
|
||||
buffer.append("Numeric.").append(name).append("(");
|
||||
buffer.append(subscript.integerValue().intValue());
|
||||
buffer.append(", new Numeric[] {");
|
||||
for(int i=0;i<parameter.length;i++) {
|
||||
buffer.append(parameter[i].toJava()).append(i<parameter.length-1?", ":"");
|
||||
}
|
||||
buffer.append("})");
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
public void toMathML(MathML element, Object data) {
|
||||
MathML e1;
|
||||
int exponent=data instanceof Integer?((Integer)data).intValue():1;
|
||||
if(exponent==1) {
|
||||
e1=element.element("msub");
|
||||
nameToMathML(e1);
|
||||
subscript.toMathML(e1,null);
|
||||
element.appendChild(e1);
|
||||
} else {
|
||||
e1=element.element("msubsup");
|
||||
nameToMathML(e1);
|
||||
subscript.toMathML(e1,null);
|
||||
MathML e2=element.element("mn");
|
||||
e2.appendChild(element.text(String.valueOf(exponent)));
|
||||
e1.appendChild(e2);
|
||||
element.appendChild(e1);
|
||||
}
|
||||
e1=element.element("mfenced");
|
||||
for(int i=0;i<parameter.length;i++) {
|
||||
parameter[i].toMathML(e1,null);
|
||||
}
|
||||
element.appendChild(e1);
|
||||
}
|
||||
|
||||
void bodyToMathML(MathML element, boolean fenced) {}
|
||||
|
||||
protected Variable newinstance() {
|
||||
return new Root(new Generic[parameter.length],null);
|
||||
}
|
||||
}
|
||||
|
||||
class Sigma {
|
||||
Generic root[];
|
||||
Generic generic;
|
||||
boolean place[];
|
||||
int n;
|
||||
|
||||
Sigma(Generic parameter[], int n) {
|
||||
root=new Generic[parameter.length-1];
|
||||
for(int i=0;i<root.length;i++) root[i]=new Root(parameter,i).expressionValue();
|
||||
place=new boolean[root.length];
|
||||
this.n=n;
|
||||
}
|
||||
|
||||
void compute() {
|
||||
generic=JSCLInteger.valueOf(0);
|
||||
compute(0,n);
|
||||
}
|
||||
|
||||
void compute(int p, int nn) {
|
||||
if(nn>0) {
|
||||
for(int i=p;i<root.length;i++) {
|
||||
place[i]=true;
|
||||
compute(i+1,nn-1);
|
||||
place[i]=false;
|
||||
}
|
||||
} else {
|
||||
Generic s=JSCLInteger.valueOf(1);
|
||||
for(int i=0;i<root.length;i++) {
|
||||
if(place[i]) s=s.multiply(root[i]);
|
||||
}
|
||||
generic=generic.add(s);
|
||||
}
|
||||
}
|
||||
|
||||
Generic getValue() {
|
||||
return generic;
|
||||
}
|
||||
}
|
78
src/misc/src/jscl/math/function/Sgn.java
Normal file
78
src/misc/src/jscl/math/function/Sgn.java
Normal file
@ -0,0 +1,78 @@
|
||||
package jscl.math.function;
|
||||
|
||||
import jscl.math.Generic;
|
||||
import jscl.math.JSCLInteger;
|
||||
import jscl.math.NotIntegerException;
|
||||
import jscl.math.NotIntegrableException;
|
||||
import jscl.math.NotVariableException;
|
||||
import jscl.math.NumericWrapper;
|
||||
import jscl.math.Variable;
|
||||
|
||||
public class Sgn extends Function {
|
||||
public Sgn(Generic generic) {
|
||||
super("sgn",new Generic[] {generic});
|
||||
}
|
||||
|
||||
public Generic antiderivative(int n) throws NotIntegrableException {
|
||||
return new Abs(parameter[0]).evaluate();
|
||||
}
|
||||
|
||||
public Generic derivative(int n) {
|
||||
return JSCLInteger.valueOf(0);
|
||||
}
|
||||
|
||||
public Generic evaluate() {
|
||||
if(parameter[0].signum()<0) {
|
||||
return new Sgn(parameter[0].negate()).evaluate().negate();
|
||||
} else if(parameter[0].signum()==0) {
|
||||
return JSCLInteger.valueOf(1);
|
||||
}
|
||||
try {
|
||||
return JSCLInteger.valueOf(parameter[0].integerValue().signum());
|
||||
} catch (NotIntegerException e) {}
|
||||
return expressionValue();
|
||||
}
|
||||
|
||||
public Generic evalelem() {
|
||||
return new Frac(
|
||||
parameter[0],
|
||||
new Abs(parameter[0]).evalelem()
|
||||
).evalelem();
|
||||
}
|
||||
|
||||
public Generic evalsimp() {
|
||||
if(parameter[0].signum()<0) {
|
||||
return new Sgn(parameter[0].negate()).evaluate().negate();
|
||||
} else if(parameter[0].signum()==0) {
|
||||
return JSCLInteger.valueOf(1);
|
||||
}
|
||||
try {
|
||||
return JSCLInteger.valueOf(parameter[0].integerValue().signum());
|
||||
} catch (NotIntegerException e) {}
|
||||
try {
|
||||
Variable v=parameter[0].variableValue();
|
||||
if(v instanceof Abs) {
|
||||
return JSCLInteger.valueOf(1);
|
||||
} else if(v instanceof Sgn) {
|
||||
Function f=(Function)v;
|
||||
return f.evalsimp();
|
||||
}
|
||||
} catch (NotVariableException e) {}
|
||||
return expressionValue();
|
||||
}
|
||||
|
||||
public Generic evalnum() {
|
||||
return ((NumericWrapper)parameter[0]).sgn();
|
||||
}
|
||||
|
||||
public String toJava() {
|
||||
StringBuffer buffer=new StringBuffer();
|
||||
buffer.append(parameter[0].toJava());
|
||||
buffer.append(".sgn()");
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
protected Variable newinstance() {
|
||||
return new Sgn(null);
|
||||
}
|
||||
}
|
123
src/misc/src/jscl/math/function/Sqrt.java
Normal file
123
src/misc/src/jscl/math/function/Sqrt.java
Normal file
@ -0,0 +1,123 @@
|
||||
package jscl.math.function;
|
||||
|
||||
import jscl.math.Antiderivative;
|
||||
import jscl.math.Generic;
|
||||
import jscl.math.JSCLInteger;
|
||||
import jscl.math.NotIntegerException;
|
||||
import jscl.math.NotIntegrableException;
|
||||
import jscl.math.NumericWrapper;
|
||||
import jscl.math.Power;
|
||||
import jscl.math.Variable;
|
||||
import jscl.mathml.MathML;
|
||||
|
||||
public class Sqrt extends Algebraic {
|
||||
public Sqrt(Generic generic) {
|
||||
super("sqrt",new Generic[] {generic});
|
||||
}
|
||||
|
||||
public Root rootValue() {
|
||||
return new Root(
|
||||
new Generic[] {
|
||||
parameter[0].negate(),
|
||||
JSCLInteger.valueOf(0),
|
||||
JSCLInteger.valueOf(1)
|
||||
},
|
||||
0
|
||||
);
|
||||
}
|
||||
|
||||
public Generic antiderivative(Variable variable) throws NotIntegrableException {
|
||||
Root r=rootValue();
|
||||
Generic g[]=r.parameters();
|
||||
if(g[0].isPolynomial(variable)) {
|
||||
return Antiderivative.compute(r,variable);
|
||||
} else throw new NotIntegrableException();
|
||||
}
|
||||
|
||||
public Generic derivative(int n) {
|
||||
return Constant.half.multiply(
|
||||
new Inv(
|
||||
evaluate()
|
||||
).evaluate()
|
||||
);
|
||||
}
|
||||
|
||||
public boolean imaginary() {
|
||||
return parameter[0].compareTo(JSCLInteger.valueOf(-1))==0;
|
||||
}
|
||||
|
||||
public Generic evaluate() {
|
||||
try {
|
||||
JSCLInteger en=parameter[0].integerValue();
|
||||
if(en.signum()<0);
|
||||
else {
|
||||
Generic rt=en.sqrt();
|
||||
if(rt.pow(2).compareTo(en)==0) return rt;
|
||||
}
|
||||
} catch (NotIntegerException e) {}
|
||||
return expressionValue();
|
||||
}
|
||||
|
||||
public Generic evalelem() {
|
||||
return evaluate();
|
||||
}
|
||||
|
||||
public Generic evalsimp() {
|
||||
try {
|
||||
JSCLInteger en=parameter[0].integerValue();
|
||||
if(en.signum()<0) return Constant.i.multiply(new Sqrt(en.negate()).evalsimp());
|
||||
else {
|
||||
Generic rt=en.sqrt();
|
||||
if(rt.pow(2).compareTo(en)==0) return rt;
|
||||
}
|
||||
Generic a=en.factorize();
|
||||
Generic p[]=a.productValue();
|
||||
Generic s=JSCLInteger.valueOf(1);
|
||||
for(int i=0;i<p.length;i++) {
|
||||
Power o=p[i].powerValue();
|
||||
Generic q=o.value(true);
|
||||
int c=o.exponent();
|
||||
s=s.multiply(q.pow(c/2).multiply(new Sqrt(q).expressionValue().pow(c%2)));
|
||||
}
|
||||
return s;
|
||||
} catch (NotIntegerException e) {
|
||||
Generic n[]=Frac.separateCoefficient(parameter[0]);
|
||||
if(n[0].compareTo(JSCLInteger.valueOf(1))==0 && n[1].compareTo(JSCLInteger.valueOf(1))==0);
|
||||
else return new Sqrt(n[2]).evalsimp().multiply(
|
||||
new Frac(
|
||||
new Sqrt(n[0]).evalsimp(),
|
||||
new Sqrt(n[1]).evalsimp()
|
||||
).evalsimp()
|
||||
);
|
||||
}
|
||||
return expressionValue();
|
||||
}
|
||||
|
||||
public Generic evalnum() {
|
||||
return ((NumericWrapper)parameter[0]).sqrt();
|
||||
}
|
||||
|
||||
public String toJava() {
|
||||
if(parameter[0].compareTo(JSCLInteger.valueOf(-1))==0) return "Complex.valueOf(0, 1)";
|
||||
StringBuffer buffer=new StringBuffer();
|
||||
buffer.append(parameter[0].toJava());
|
||||
buffer.append(".").append(name).append("()");
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
void bodyToMathML(MathML element, boolean fenced) {
|
||||
if(parameter[0].compareTo(JSCLInteger.valueOf(-1))==0) {
|
||||
MathML e1=element.element("mi");
|
||||
e1.appendChild(element.text(/*"\u2148"*/"i"));
|
||||
element.appendChild(e1);
|
||||
} else {
|
||||
MathML e1=element.element("msqrt");
|
||||
parameter[0].toMathML(e1,null);
|
||||
element.appendChild(e1);
|
||||
}
|
||||
}
|
||||
|
||||
protected Variable newinstance() {
|
||||
return new Sqrt(null);
|
||||
}
|
||||
}
|
41
src/misc/src/jscl/math/function/Trigonometric.java
Normal file
41
src/misc/src/jscl/math/function/Trigonometric.java
Normal file
@ -0,0 +1,41 @@
|
||||
package jscl.math.function;
|
||||
|
||||
import jscl.math.Generic;
|
||||
import jscl.math.NotIntegrableException;
|
||||
import jscl.math.Variable;
|
||||
import jscl.math.polynomial.Polynomial;
|
||||
|
||||
public abstract class Trigonometric extends Function {
|
||||
public Trigonometric(String name, Generic parameter[]) {
|
||||
super(name,parameter);
|
||||
}
|
||||
|
||||
public Generic antiderivative(Variable variable) throws NotIntegrableException {
|
||||
Generic s=parameter[0];
|
||||
if(s.isPolynomial(variable)) {
|
||||
Polynomial p=Polynomial.factory(variable).valueof(s);
|
||||
if(p.degree()==1) {
|
||||
Generic a[]=p.elements();
|
||||
return new Inv(a[1]).evaluate().multiply(antiderivative(0));
|
||||
} else throw new NotIntegrableException();
|
||||
} else throw new NotIntegrableException();
|
||||
}
|
||||
|
||||
public Generic identity() {
|
||||
// Generic a[]=parameter[0].sumValue();
|
||||
// if(a.length>1) {
|
||||
// Generic s=JSCLInteger.valueOf(0);
|
||||
// for(int i=1;i<a.length;i++) s=s.add(a[i]);
|
||||
// return identity(a[0],s);
|
||||
// }
|
||||
// Generic n[]=Frac.separateCoefficient(parameter[0]);
|
||||
// if(n[0].compareTo(JSCLInteger.valueOf(1))==0);
|
||||
// else {
|
||||
// Generic s=new Frac(n[2],n[1]).evalsimp();
|
||||
// return identity(s,n[0].subtract(JSCLInteger.valueOf(1)).multiply(s));
|
||||
// }
|
||||
return expressionValue();
|
||||
}
|
||||
|
||||
public abstract Generic identity(Generic a, Generic b);
|
||||
}
|
55
src/misc/src/jscl/math/function/hyperbolic/Acosh.java
Normal file
55
src/misc/src/jscl/math/function/hyperbolic/Acosh.java
Normal file
@ -0,0 +1,55 @@
|
||||
package jscl.math.function.hyperbolic;
|
||||
|
||||
import jscl.math.Generic;
|
||||
import jscl.math.JSCLInteger;
|
||||
import jscl.math.NumericWrapper;
|
||||
import jscl.math.Variable;
|
||||
import jscl.math.function.ArcTrigonometric;
|
||||
import jscl.math.function.Inv;
|
||||
import jscl.math.function.Log;
|
||||
import jscl.math.function.Root;
|
||||
import jscl.math.function.Sqrt;
|
||||
|
||||
public class Acosh extends ArcTrigonometric {
|
||||
public Acosh(Generic generic) {
|
||||
super("acosh",new Generic[] {generic});
|
||||
}
|
||||
|
||||
public Generic derivative(int n) {
|
||||
return new Inv(
|
||||
new Sqrt(
|
||||
parameter[0].pow(2).subtract(
|
||||
JSCLInteger.valueOf(1)
|
||||
)
|
||||
).evaluate()
|
||||
).evaluate();
|
||||
}
|
||||
|
||||
public Generic evaluate() {
|
||||
if(parameter[0].signum()==0) {
|
||||
return JSCLInteger.valueOf(0);
|
||||
}
|
||||
return expressionValue();
|
||||
}
|
||||
|
||||
public Generic evalelem() {
|
||||
return new Log(
|
||||
new Root(
|
||||
new Generic[] {
|
||||
JSCLInteger.valueOf(-1),
|
||||
JSCLInteger.valueOf(2).multiply(parameter[0]),
|
||||
JSCLInteger.valueOf(-1)
|
||||
},
|
||||
0
|
||||
).evalelem()
|
||||
).evalelem();
|
||||
}
|
||||
|
||||
public Generic evalnum() {
|
||||
return ((NumericWrapper)parameter[0]).acosh();
|
||||
}
|
||||
|
||||
protected Variable newinstance() {
|
||||
return new Acosh(null);
|
||||
}
|
||||
}
|
52
src/misc/src/jscl/math/function/hyperbolic/Acoth.java
Normal file
52
src/misc/src/jscl/math/function/hyperbolic/Acoth.java
Normal file
@ -0,0 +1,52 @@
|
||||
package jscl.math.function.hyperbolic;
|
||||
|
||||
import jscl.math.Generic;
|
||||
import jscl.math.JSCLInteger;
|
||||
import jscl.math.NumericWrapper;
|
||||
import jscl.math.Variable;
|
||||
import jscl.math.function.ArcTrigonometric;
|
||||
import jscl.math.function.Inv;
|
||||
import jscl.math.function.Log;
|
||||
import jscl.math.function.Root;
|
||||
|
||||
public class Acoth extends ArcTrigonometric {
|
||||
public Acoth(Generic generic) {
|
||||
super("acoth",new Generic[] {generic});
|
||||
}
|
||||
|
||||
public Generic derivative(int n) {
|
||||
return new Inv(
|
||||
JSCLInteger.valueOf(1).subtract(
|
||||
parameter[0].pow(2)
|
||||
)
|
||||
).evaluate();
|
||||
}
|
||||
|
||||
public Generic evaluate() {
|
||||
if(parameter[0].signum()<0) {
|
||||
return new Acoth(parameter[0].negate()).evaluate().negate();
|
||||
}
|
||||
return expressionValue();
|
||||
}
|
||||
|
||||
public Generic evalelem() {
|
||||
return new Log(
|
||||
new Root(
|
||||
new Generic[] {
|
||||
JSCLInteger.valueOf(1).add(parameter[0]),
|
||||
JSCLInteger.valueOf(0),
|
||||
JSCLInteger.valueOf(1).subtract(parameter[0])
|
||||
},
|
||||
0
|
||||
).evalelem()
|
||||
).evalelem();
|
||||
}
|
||||
|
||||
public Generic evalnum() {
|
||||
return ((NumericWrapper)parameter[0]).acoth();
|
||||
}
|
||||
|
||||
protected Variable newinstance() {
|
||||
return new Acoth(null);
|
||||
}
|
||||
}
|
57
src/misc/src/jscl/math/function/hyperbolic/Asinh.java
Normal file
57
src/misc/src/jscl/math/function/hyperbolic/Asinh.java
Normal file
@ -0,0 +1,57 @@
|
||||
package jscl.math.function.hyperbolic;
|
||||
|
||||
import jscl.math.Generic;
|
||||
import jscl.math.JSCLInteger;
|
||||
import jscl.math.NumericWrapper;
|
||||
import jscl.math.Variable;
|
||||
import jscl.math.function.ArcTrigonometric;
|
||||
import jscl.math.function.Inv;
|
||||
import jscl.math.function.Log;
|
||||
import jscl.math.function.Root;
|
||||
import jscl.math.function.Sqrt;
|
||||
|
||||
public class Asinh extends ArcTrigonometric {
|
||||
public Asinh(Generic generic) {
|
||||
super("asinh",new Generic[] {generic});
|
||||
}
|
||||
|
||||
public Generic derivative(int n) {
|
||||
return new Inv(
|
||||
new Sqrt(
|
||||
JSCLInteger.valueOf(1).add(
|
||||
parameter[0].pow(2)
|
||||
)
|
||||
).evaluate()
|
||||
).evaluate();
|
||||
}
|
||||
|
||||
public Generic evaluate() {
|
||||
if(parameter[0].signum()<0) {
|
||||
return new Asinh(parameter[0].negate()).evaluate().negate();
|
||||
} else if(parameter[0].signum()==0) {
|
||||
return JSCLInteger.valueOf(0);
|
||||
}
|
||||
return expressionValue();
|
||||
}
|
||||
|
||||
public Generic evalelem() {
|
||||
return new Log(
|
||||
new Root(
|
||||
new Generic[] {
|
||||
JSCLInteger.valueOf(1),
|
||||
JSCLInteger.valueOf(2).multiply(parameter[0]),
|
||||
JSCLInteger.valueOf(-1)
|
||||
},
|
||||
0
|
||||
).evalelem()
|
||||
).evalelem();
|
||||
}
|
||||
|
||||
public Generic evalnum() {
|
||||
return ((NumericWrapper)parameter[0]).asinh();
|
||||
}
|
||||
|
||||
protected Variable newinstance() {
|
||||
return new Asinh(null);
|
||||
}
|
||||
}
|
54
src/misc/src/jscl/math/function/hyperbolic/Atanh.java
Normal file
54
src/misc/src/jscl/math/function/hyperbolic/Atanh.java
Normal file
@ -0,0 +1,54 @@
|
||||
package jscl.math.function.hyperbolic;
|
||||
|
||||
import jscl.math.Generic;
|
||||
import jscl.math.JSCLInteger;
|
||||
import jscl.math.NumericWrapper;
|
||||
import jscl.math.Variable;
|
||||
import jscl.math.function.ArcTrigonometric;
|
||||
import jscl.math.function.Inv;
|
||||
import jscl.math.function.Log;
|
||||
import jscl.math.function.Root;
|
||||
|
||||
public class Atanh extends ArcTrigonometric {
|
||||
public Atanh(Generic generic) {
|
||||
super("atanh",new Generic[] {generic});
|
||||
}
|
||||
|
||||
public Generic derivative(int n) {
|
||||
return new Inv(
|
||||
JSCLInteger.valueOf(1).subtract(
|
||||
parameter[0].pow(2)
|
||||
)
|
||||
).evaluate();
|
||||
}
|
||||
|
||||
public Generic evaluate() {
|
||||
if(parameter[0].signum()<0) {
|
||||
return new Atanh(parameter[0].negate()).evaluate().negate();
|
||||
} else if(parameter[0].signum()==0) {
|
||||
return JSCLInteger.valueOf(0);
|
||||
}
|
||||
return expressionValue();
|
||||
}
|
||||
|
||||
public Generic evalelem() {
|
||||
return new Log(
|
||||
new Root(
|
||||
new Generic[] {
|
||||
JSCLInteger.valueOf(1).add(parameter[0]),
|
||||
JSCLInteger.valueOf(0),
|
||||
JSCLInteger.valueOf(-1).add(parameter[0])
|
||||
},
|
||||
0
|
||||
).evalelem()
|
||||
).evalelem();
|
||||
}
|
||||
|
||||
public Generic evalnum() {
|
||||
return ((NumericWrapper)parameter[0]).atanh();
|
||||
}
|
||||
|
||||
protected Variable newinstance() {
|
||||
return new Atanh(null);
|
||||
}
|
||||
}
|
78
src/misc/src/jscl/math/function/hyperbolic/Cosh.java
Normal file
78
src/misc/src/jscl/math/function/hyperbolic/Cosh.java
Normal file
@ -0,0 +1,78 @@
|
||||
package jscl.math.function.hyperbolic;
|
||||
|
||||
import jscl.math.Generic;
|
||||
import jscl.math.JSCLInteger;
|
||||
import jscl.math.NotIntegrableException;
|
||||
import jscl.math.NotVariableException;
|
||||
import jscl.math.NumericWrapper;
|
||||
import jscl.math.Variable;
|
||||
import jscl.math.function.Constant;
|
||||
import jscl.math.function.Exp;
|
||||
import jscl.math.function.Trigonometric;
|
||||
|
||||
public class Cosh extends Trigonometric {
|
||||
public Cosh(Generic generic) {
|
||||
super("cosh",new Generic[] {generic});
|
||||
}
|
||||
|
||||
public Generic antiderivative(int n) throws NotIntegrableException {
|
||||
return new Sinh(parameter[0]).evaluate();
|
||||
}
|
||||
|
||||
public Generic derivative(int n) {
|
||||
return new Sinh(parameter[0]).evaluate();
|
||||
}
|
||||
|
||||
public Generic evaluate() {
|
||||
if(parameter[0].signum()<0) {
|
||||
return new Cosh(parameter[0].negate()).evaluate();
|
||||
} else if(parameter[0].signum()==0) {
|
||||
return JSCLInteger.valueOf(1);
|
||||
}
|
||||
return expressionValue();
|
||||
}
|
||||
|
||||
public Generic evalelem() {
|
||||
return new Exp(
|
||||
parameter[0]
|
||||
).evalelem().add(
|
||||
new Exp(
|
||||
parameter[0].negate()
|
||||
).evalelem()
|
||||
).multiply(Constant.half);
|
||||
}
|
||||
|
||||
public Generic evalsimp() {
|
||||
if(parameter[0].signum()<0) {
|
||||
return new Cosh(parameter[0].negate()).evaluate();
|
||||
} else if(parameter[0].signum()==0) {
|
||||
return JSCLInteger.valueOf(1);
|
||||
}
|
||||
try {
|
||||
Variable v=parameter[0].variableValue();
|
||||
if(v instanceof Acosh) {
|
||||
Generic g[]=((Acosh)v).parameters();
|
||||
return g[0];
|
||||
}
|
||||
} catch (NotVariableException e) {}
|
||||
return identity();
|
||||
}
|
||||
|
||||
public Generic identity(Generic a, Generic b) {
|
||||
return new Cosh(a).evalsimp().multiply(
|
||||
new Cosh(b).evalsimp()
|
||||
).add(
|
||||
new Sinh(a).evalsimp().multiply(
|
||||
new Sinh(b).evalsimp()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public Generic evalnum() {
|
||||
return ((NumericWrapper)parameter[0]).cosh();
|
||||
}
|
||||
|
||||
protected Variable newinstance() {
|
||||
return new Cosh(null);
|
||||
}
|
||||
}
|
76
src/misc/src/jscl/math/function/hyperbolic/Coth.java
Normal file
76
src/misc/src/jscl/math/function/hyperbolic/Coth.java
Normal file
@ -0,0 +1,76 @@
|
||||
package jscl.math.function.hyperbolic;
|
||||
|
||||
import jscl.math.Generic;
|
||||
import jscl.math.JSCLInteger;
|
||||
import jscl.math.NotIntegrableException;
|
||||
import jscl.math.NotVariableException;
|
||||
import jscl.math.NumericWrapper;
|
||||
import jscl.math.Variable;
|
||||
import jscl.math.function.Frac;
|
||||
import jscl.math.function.Log;
|
||||
import jscl.math.function.Trigonometric;
|
||||
|
||||
public class Coth extends Trigonometric {
|
||||
public Coth(Generic generic) {
|
||||
super("coth",new Generic[] {generic});
|
||||
}
|
||||
|
||||
public Generic antiderivative(int n) throws NotIntegrableException {
|
||||
return new Log(
|
||||
JSCLInteger.valueOf(4).multiply(
|
||||
new Sinh(parameter[0]).evaluate()
|
||||
)
|
||||
).evaluate();
|
||||
}
|
||||
|
||||
public Generic derivative(int n) {
|
||||
return JSCLInteger.valueOf(1).subtract(
|
||||
new Coth(parameter[0]).evaluate().pow(2)
|
||||
);
|
||||
}
|
||||
|
||||
public Generic evaluate() {
|
||||
if(parameter[0].signum()<0) {
|
||||
return new Coth(parameter[0].negate()).evaluate().negate();
|
||||
}
|
||||
return expressionValue();
|
||||
}
|
||||
|
||||
public Generic evalelem() {
|
||||
return new Frac(
|
||||
new Cosh(parameter[0]).evalelem(),
|
||||
new Sinh(parameter[0]).evalelem()
|
||||
).evalelem();
|
||||
}
|
||||
|
||||
public Generic evalsimp() {
|
||||
if(parameter[0].signum()<0) {
|
||||
return new Coth(parameter[0].negate()).evaluate().negate();
|
||||
}
|
||||
try {
|
||||
Variable v=parameter[0].variableValue();
|
||||
if(v instanceof Acoth) {
|
||||
Generic g[]=((Acoth)v).parameters();
|
||||
return g[0];
|
||||
}
|
||||
} catch (NotVariableException e) {}
|
||||
return identity();
|
||||
}
|
||||
|
||||
public Generic identity(Generic a, Generic b) {
|
||||
Generic ta=new Coth(a).evalsimp();
|
||||
Generic tb=new Coth(b).evalsimp();
|
||||
return new Frac(
|
||||
ta.multiply(tb).add(JSCLInteger.valueOf(1)),
|
||||
ta.add(tb)
|
||||
).evalsimp();
|
||||
}
|
||||
|
||||
public Generic evalnum() {
|
||||
return ((NumericWrapper)parameter[0]).coth();
|
||||
}
|
||||
|
||||
protected Variable newinstance() {
|
||||
return new Coth(null);
|
||||
}
|
||||
}
|
78
src/misc/src/jscl/math/function/hyperbolic/Sinh.java
Normal file
78
src/misc/src/jscl/math/function/hyperbolic/Sinh.java
Normal file
@ -0,0 +1,78 @@
|
||||
package jscl.math.function.hyperbolic;
|
||||
|
||||
import jscl.math.Generic;
|
||||
import jscl.math.JSCLInteger;
|
||||
import jscl.math.NotIntegrableException;
|
||||
import jscl.math.NotVariableException;
|
||||
import jscl.math.NumericWrapper;
|
||||
import jscl.math.Variable;
|
||||
import jscl.math.function.Constant;
|
||||
import jscl.math.function.Exp;
|
||||
import jscl.math.function.Trigonometric;
|
||||
|
||||
public class Sinh extends Trigonometric {
|
||||
public Sinh(Generic generic) {
|
||||
super("sinh",new Generic[] {generic});
|
||||
}
|
||||
|
||||
public Generic antiderivative(int n) throws NotIntegrableException {
|
||||
return new Cosh(parameter[0]).evaluate();
|
||||
}
|
||||
|
||||
public Generic derivative(int n) {
|
||||
return new Cosh(parameter[0]).evaluate();
|
||||
}
|
||||
|
||||
public Generic evaluate() {
|
||||
if(parameter[0].signum()<0) {
|
||||
return new Sinh(parameter[0].negate()).evaluate().negate();
|
||||
} else if(parameter[0].signum()==0) {
|
||||
return JSCLInteger.valueOf(0);
|
||||
}
|
||||
return expressionValue();
|
||||
}
|
||||
|
||||
public Generic evalelem() {
|
||||
return new Exp(
|
||||
parameter[0]
|
||||
).evalelem().subtract(
|
||||
new Exp(
|
||||
parameter[0].negate()
|
||||
).evalelem()
|
||||
).multiply(Constant.half);
|
||||
}
|
||||
|
||||
public Generic evalsimp() {
|
||||
if(parameter[0].signum()<0) {
|
||||
return new Sinh(parameter[0].negate()).evaluate().negate();
|
||||
} else if(parameter[0].signum()==0) {
|
||||
return JSCLInteger.valueOf(0);
|
||||
}
|
||||
try {
|
||||
Variable v=parameter[0].variableValue();
|
||||
if(v instanceof Asinh) {
|
||||
Generic g[]=((Asinh)v).parameters();
|
||||
return g[0];
|
||||
}
|
||||
} catch (NotVariableException e) {}
|
||||
return identity();
|
||||
}
|
||||
|
||||
public Generic identity(Generic a, Generic b) {
|
||||
return new Cosh(b).evalsimp().multiply(
|
||||
new Sinh(a).evalsimp()
|
||||
).add(
|
||||
new Cosh(a).evalsimp().multiply(
|
||||
new Sinh(b).evalsimp()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public Generic evalnum() {
|
||||
return ((NumericWrapper)parameter[0]).sinh();
|
||||
}
|
||||
|
||||
protected Variable newinstance() {
|
||||
return new Sinh(null);
|
||||
}
|
||||
}
|
82
src/misc/src/jscl/math/function/hyperbolic/Tanh.java
Normal file
82
src/misc/src/jscl/math/function/hyperbolic/Tanh.java
Normal file
@ -0,0 +1,82 @@
|
||||
package jscl.math.function.hyperbolic;
|
||||
|
||||
import jscl.math.Generic;
|
||||
import jscl.math.JSCLInteger;
|
||||
import jscl.math.NotIntegrableException;
|
||||
import jscl.math.NotVariableException;
|
||||
import jscl.math.NumericWrapper;
|
||||
import jscl.math.Variable;
|
||||
import jscl.math.function.Frac;
|
||||
import jscl.math.function.Log;
|
||||
import jscl.math.function.Trigonometric;
|
||||
|
||||
public class Tanh extends Trigonometric {
|
||||
public Tanh(Generic generic) {
|
||||
super("tanh",new Generic[] {generic});
|
||||
}
|
||||
|
||||
public Generic antiderivative(int n) throws NotIntegrableException {
|
||||
return new Log(
|
||||
JSCLInteger.valueOf(4).multiply(
|
||||
new Cosh(parameter[0]).evaluate()
|
||||
)
|
||||
).evaluate();
|
||||
}
|
||||
|
||||
public Generic derivative(int n) {
|
||||
return JSCLInteger.valueOf(1).subtract(
|
||||
new Tanh(parameter[0]).evaluate().pow(2)
|
||||
);
|
||||
}
|
||||
|
||||
public Generic evaluate() {
|
||||
if(parameter[0].signum()<0) {
|
||||
return new Tanh(parameter[0].negate()).evaluate().negate();
|
||||
} else if(parameter[0].signum()==0) {
|
||||
return JSCLInteger.valueOf(0);
|
||||
}
|
||||
return expressionValue();
|
||||
}
|
||||
|
||||
public Generic evalelem() {
|
||||
return new Frac(
|
||||
new Sinh(parameter[0]).evalelem(),
|
||||
new Cosh(parameter[0]).evalelem()
|
||||
).evalelem();
|
||||
}
|
||||
|
||||
public Generic evalsimp() {
|
||||
if(parameter[0].signum()<0) {
|
||||
return new Tanh(parameter[0].negate()).evaluate().negate();
|
||||
} else if(parameter[0].signum()==0) {
|
||||
return JSCLInteger.valueOf(0);
|
||||
}
|
||||
try {
|
||||
Variable v=parameter[0].variableValue();
|
||||
if(v instanceof Atanh) {
|
||||
Generic g[]=((Atanh)v).parameters();
|
||||
return g[0];
|
||||
}
|
||||
} catch (NotVariableException e) {}
|
||||
return identity();
|
||||
}
|
||||
|
||||
public Generic identity(Generic a, Generic b) {
|
||||
Generic ta=new Tanh(a).evalsimp();
|
||||
Generic tb=new Tanh(b).evalsimp();
|
||||
return new Frac(
|
||||
ta.add(tb),
|
||||
JSCLInteger.valueOf(1).add(
|
||||
ta.multiply(tb)
|
||||
)
|
||||
).evalsimp();
|
||||
}
|
||||
|
||||
public Generic evalnum() {
|
||||
return ((NumericWrapper)parameter[0]).tanh();
|
||||
}
|
||||
|
||||
protected Variable newinstance() {
|
||||
return new Tanh(null);
|
||||
}
|
||||
}
|
58
src/misc/src/jscl/math/function/trigonometric/Acos.java
Normal file
58
src/misc/src/jscl/math/function/trigonometric/Acos.java
Normal file
@ -0,0 +1,58 @@
|
||||
package jscl.math.function.trigonometric;
|
||||
|
||||
import jscl.math.Generic;
|
||||
import jscl.math.JSCLInteger;
|
||||
import jscl.math.NumericWrapper;
|
||||
import jscl.math.Variable;
|
||||
import jscl.math.function.ArcTrigonometric;
|
||||
import jscl.math.function.Constant;
|
||||
import jscl.math.function.Inv;
|
||||
import jscl.math.function.Log;
|
||||
import jscl.math.function.Root;
|
||||
import jscl.math.function.Sqrt;
|
||||
|
||||
public class Acos extends ArcTrigonometric {
|
||||
public Acos(Generic generic) {
|
||||
super("acos",new Generic[] {generic});
|
||||
}
|
||||
|
||||
public Generic derivative(int n) {
|
||||
return new Inv(
|
||||
new Sqrt(
|
||||
JSCLInteger.valueOf(1).subtract(parameter[0].pow(2))
|
||||
).evaluate()
|
||||
).evaluate().negate();
|
||||
}
|
||||
|
||||
public Generic evaluate() {
|
||||
if(parameter[0].signum()<0) {
|
||||
return Constant.pi.subtract(new Acos(parameter[0].negate()).evaluate());
|
||||
} else if(parameter[0].compareTo(JSCLInteger.valueOf(1))==0) {
|
||||
return JSCLInteger.valueOf(0);
|
||||
}
|
||||
return expressionValue();
|
||||
}
|
||||
|
||||
public Generic evalelem() {
|
||||
return Constant.i.multiply(
|
||||
new Log(
|
||||
new Root(
|
||||
new Generic[] {
|
||||
JSCLInteger.valueOf(-1),
|
||||
JSCLInteger.valueOf(2).multiply(parameter[0]),
|
||||
JSCLInteger.valueOf(-1)
|
||||
},
|
||||
0
|
||||
).evalelem()
|
||||
).evalelem()
|
||||
);
|
||||
}
|
||||
|
||||
public Generic evalnum() {
|
||||
return ((NumericWrapper)parameter[0]).acos();
|
||||
}
|
||||
|
||||
protected Variable newinstance() {
|
||||
return new Acos(null);
|
||||
}
|
||||
}
|
53
src/misc/src/jscl/math/function/trigonometric/Acot.java
Normal file
53
src/misc/src/jscl/math/function/trigonometric/Acot.java
Normal file
@ -0,0 +1,53 @@
|
||||
package jscl.math.function.trigonometric;
|
||||
|
||||
import jscl.math.Generic;
|
||||
import jscl.math.JSCLInteger;
|
||||
import jscl.math.NumericWrapper;
|
||||
import jscl.math.Variable;
|
||||
import jscl.math.function.ArcTrigonometric;
|
||||
import jscl.math.function.Constant;
|
||||
import jscl.math.function.Inv;
|
||||
import jscl.math.function.Log;
|
||||
import jscl.math.function.Root;
|
||||
|
||||
public class Acot extends ArcTrigonometric {
|
||||
public Acot(Generic generic) {
|
||||
super("acot",new Generic[] {generic});
|
||||
}
|
||||
|
||||
public Generic derivative(int n) {
|
||||
return new Inv(
|
||||
JSCLInteger.valueOf(1).add(parameter[0].pow(2))
|
||||
).evaluate().negate();
|
||||
}
|
||||
|
||||
public Generic evaluate() {
|
||||
if(parameter[0].signum()<0) {
|
||||
return Constant.pi.subtract(new Acot(parameter[0].negate()).evaluate());
|
||||
}
|
||||
return expressionValue();
|
||||
}
|
||||
|
||||
public Generic evalelem() {
|
||||
return Constant.i.multiply(
|
||||
new Log(
|
||||
new Root(
|
||||
new Generic[] {
|
||||
Constant.i.add(parameter[0]),
|
||||
JSCLInteger.valueOf(0),
|
||||
Constant.i.subtract(parameter[0])
|
||||
},
|
||||
0
|
||||
).evalelem()
|
||||
).evalelem()
|
||||
);
|
||||
}
|
||||
|
||||
public Generic evalnum() {
|
||||
return ((NumericWrapper)parameter[0]).acot();
|
||||
}
|
||||
|
||||
protected Variable newinstance() {
|
||||
return new Acot(null);
|
||||
}
|
||||
}
|
58
src/misc/src/jscl/math/function/trigonometric/Asin.java
Normal file
58
src/misc/src/jscl/math/function/trigonometric/Asin.java
Normal file
@ -0,0 +1,58 @@
|
||||
package jscl.math.function.trigonometric;
|
||||
|
||||
import jscl.math.Generic;
|
||||
import jscl.math.JSCLInteger;
|
||||
import jscl.math.NumericWrapper;
|
||||
import jscl.math.Variable;
|
||||
import jscl.math.function.ArcTrigonometric;
|
||||
import jscl.math.function.Constant;
|
||||
import jscl.math.function.Inv;
|
||||
import jscl.math.function.Log;
|
||||
import jscl.math.function.Root;
|
||||
import jscl.math.function.Sqrt;
|
||||
|
||||
public class Asin extends ArcTrigonometric {
|
||||
public Asin(Generic generic) {
|
||||
super("asin",new Generic[] {generic});
|
||||
}
|
||||
|
||||
public Generic derivative(int n) {
|
||||
return new Inv(
|
||||
new Sqrt(
|
||||
JSCLInteger.valueOf(1).subtract(parameter[0].pow(2))
|
||||
).evaluate()
|
||||
).evaluate();
|
||||
}
|
||||
|
||||
public Generic evaluate() {
|
||||
if(parameter[0].signum()<0) {
|
||||
return new Asin(parameter[0].negate()).evaluate().negate();
|
||||
} else if(parameter[0].signum()==0) {
|
||||
return JSCLInteger.valueOf(0);
|
||||
}
|
||||
return expressionValue();
|
||||
}
|
||||
|
||||
public Generic evalelem() {
|
||||
return Constant.i.multiply(
|
||||
new Log(
|
||||
new Root(
|
||||
new Generic[] {
|
||||
JSCLInteger.valueOf(-1),
|
||||
JSCLInteger.valueOf(2).multiply(Constant.i.multiply(parameter[0])),
|
||||
JSCLInteger.valueOf(1)
|
||||
},
|
||||
0
|
||||
).evalelem()
|
||||
).evalelem()
|
||||
);
|
||||
}
|
||||
|
||||
public Generic evalnum() {
|
||||
return ((NumericWrapper)parameter[0]).asin();
|
||||
}
|
||||
|
||||
protected Variable newinstance() {
|
||||
return new Asin(null);
|
||||
}
|
||||
}
|
55
src/misc/src/jscl/math/function/trigonometric/Atan.java
Normal file
55
src/misc/src/jscl/math/function/trigonometric/Atan.java
Normal file
@ -0,0 +1,55 @@
|
||||
package jscl.math.function.trigonometric;
|
||||
|
||||
import jscl.math.Generic;
|
||||
import jscl.math.JSCLInteger;
|
||||
import jscl.math.NumericWrapper;
|
||||
import jscl.math.Variable;
|
||||
import jscl.math.function.ArcTrigonometric;
|
||||
import jscl.math.function.Constant;
|
||||
import jscl.math.function.Inv;
|
||||
import jscl.math.function.Log;
|
||||
import jscl.math.function.Root;
|
||||
|
||||
public class Atan extends ArcTrigonometric {
|
||||
public Atan(Generic generic) {
|
||||
super("atan",new Generic[] {generic});
|
||||
}
|
||||
|
||||
public Generic derivative(int n) {
|
||||
return new Inv(
|
||||
JSCLInteger.valueOf(1).add(parameter[0].pow(2))
|
||||
).evaluate();
|
||||
}
|
||||
|
||||
public Generic evaluate() {
|
||||
if(parameter[0].signum()<0) {
|
||||
return new Atan(parameter[0].negate()).evaluate().negate();
|
||||
} else if(parameter[0].signum()==0) {
|
||||
return JSCLInteger.valueOf(0);
|
||||
}
|
||||
return expressionValue();
|
||||
}
|
||||
|
||||
public Generic evalelem() {
|
||||
return Constant.i.multiply(
|
||||
new Log(
|
||||
new Root(
|
||||
new Generic[] {
|
||||
JSCLInteger.valueOf(-1).add(Constant.i.multiply(parameter[0])),
|
||||
JSCLInteger.valueOf(0),
|
||||
JSCLInteger.valueOf(1).add(Constant.i.multiply(parameter[0]))
|
||||
},
|
||||
0
|
||||
).evalelem()
|
||||
).evalelem()
|
||||
);
|
||||
}
|
||||
|
||||
public Generic evalnum() {
|
||||
return ((NumericWrapper)parameter[0]).atan();
|
||||
}
|
||||
|
||||
protected Variable newinstance() {
|
||||
return new Atan(null);
|
||||
}
|
||||
}
|
82
src/misc/src/jscl/math/function/trigonometric/Cos.java
Normal file
82
src/misc/src/jscl/math/function/trigonometric/Cos.java
Normal file
@ -0,0 +1,82 @@
|
||||
package jscl.math.function.trigonometric;
|
||||
|
||||
import jscl.math.Generic;
|
||||
import jscl.math.JSCLInteger;
|
||||
import jscl.math.NotIntegrableException;
|
||||
import jscl.math.NotVariableException;
|
||||
import jscl.math.NumericWrapper;
|
||||
import jscl.math.Variable;
|
||||
import jscl.math.function.Constant;
|
||||
import jscl.math.function.Exp;
|
||||
import jscl.math.function.Trigonometric;
|
||||
|
||||
public class Cos extends Trigonometric {
|
||||
public Cos(Generic generic) {
|
||||
super("cos",new Generic[] {generic});
|
||||
}
|
||||
|
||||
public Generic antiderivative(int n) throws NotIntegrableException {
|
||||
return new Sin(parameter[0]).evaluate();
|
||||
}
|
||||
|
||||
public Generic derivative(int n) {
|
||||
return new Sin(parameter[0]).evaluate().negate();
|
||||
}
|
||||
|
||||
public Generic evaluate() {
|
||||
if(parameter[0].signum()<0) {
|
||||
return new Cos(parameter[0].negate()).evaluate();
|
||||
} else if(parameter[0].signum()==0) {
|
||||
return JSCLInteger.valueOf(1);
|
||||
} else if(parameter[0].compareTo(Constant.pi)==0) {
|
||||
return JSCLInteger.valueOf(-1);
|
||||
}
|
||||
return expressionValue();
|
||||
}
|
||||
|
||||
public Generic evalelem() {
|
||||
return new Exp(
|
||||
Constant.i.multiply(parameter[0])
|
||||
).evalelem().add(
|
||||
new Exp(
|
||||
Constant.i.multiply(parameter[0].negate())
|
||||
).evalelem()
|
||||
).multiply(Constant.half);
|
||||
}
|
||||
|
||||
public Generic evalsimp() {
|
||||
if(parameter[0].signum()<0) {
|
||||
return new Cos(parameter[0].negate()).evaluate();
|
||||
} else if(parameter[0].signum()==0) {
|
||||
return JSCLInteger.valueOf(1);
|
||||
} else if(parameter[0].compareTo(Constant.pi)==0) {
|
||||
return JSCLInteger.valueOf(-1);
|
||||
}
|
||||
try {
|
||||
Variable v=parameter[0].variableValue();
|
||||
if(v instanceof Acos) {
|
||||
Generic g[]=((Acos)v).parameters();
|
||||
return g[0];
|
||||
}
|
||||
} catch (NotVariableException e) {}
|
||||
return identity();
|
||||
}
|
||||
|
||||
public Generic identity(Generic a, Generic b) {
|
||||
return new Cos(a).evalsimp().multiply(
|
||||
new Cos(b).evalsimp()
|
||||
).subtract(
|
||||
new Sin(a).evalsimp().multiply(
|
||||
new Sin(b).evalsimp()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public Generic evalnum() {
|
||||
return ((NumericWrapper)parameter[0]).cos();
|
||||
}
|
||||
|
||||
protected Variable newinstance() {
|
||||
return new Cos(null);
|
||||
}
|
||||
}
|
76
src/misc/src/jscl/math/function/trigonometric/Cot.java
Normal file
76
src/misc/src/jscl/math/function/trigonometric/Cot.java
Normal file
@ -0,0 +1,76 @@
|
||||
package jscl.math.function.trigonometric;
|
||||
|
||||
import jscl.math.Generic;
|
||||
import jscl.math.JSCLInteger;
|
||||
import jscl.math.NotIntegrableException;
|
||||
import jscl.math.NotVariableException;
|
||||
import jscl.math.NumericWrapper;
|
||||
import jscl.math.Variable;
|
||||
import jscl.math.function.Frac;
|
||||
import jscl.math.function.Log;
|
||||
import jscl.math.function.Trigonometric;
|
||||
|
||||
public class Cot extends Trigonometric {
|
||||
public Cot(Generic generic) {
|
||||
super("cot",new Generic[] {generic});
|
||||
}
|
||||
|
||||
public Generic antiderivative(int n) throws NotIntegrableException {
|
||||
return new Log(
|
||||
JSCLInteger.valueOf(4).multiply(
|
||||
new Sin(parameter[0]).evaluate()
|
||||
)
|
||||
).evaluate();
|
||||
}
|
||||
|
||||
public Generic derivative(int n) {
|
||||
return JSCLInteger.valueOf(1).add(
|
||||
new Cot(parameter[0]).evaluate().pow(2)
|
||||
).negate();
|
||||
}
|
||||
|
||||
public Generic evaluate() {
|
||||
if(parameter[0].signum()<0) {
|
||||
return new Cot(parameter[0].negate()).evaluate().negate();
|
||||
}
|
||||
return expressionValue();
|
||||
}
|
||||
|
||||
public Generic evalelem() {
|
||||
return new Frac(
|
||||
new Cos(parameter[0]).evalelem(),
|
||||
new Sin(parameter[0]).evalelem()
|
||||
).evalelem();
|
||||
}
|
||||
|
||||
public Generic evalsimp() {
|
||||
if(parameter[0].signum()<0) {
|
||||
return new Cot(parameter[0].negate()).evaluate().negate();
|
||||
}
|
||||
try {
|
||||
Variable v=parameter[0].variableValue();
|
||||
if(v instanceof Acot) {
|
||||
Generic g[]=((Acot)v).parameters();
|
||||
return g[0];
|
||||
}
|
||||
} catch (NotVariableException e) {}
|
||||
return identity();
|
||||
}
|
||||
|
||||
public Generic identity(Generic a, Generic b) {
|
||||
Generic ta=new Cot(a).evalsimp();
|
||||
Generic tb=new Cot(b).evalsimp();
|
||||
return new Frac(
|
||||
ta.multiply(tb).subtract(JSCLInteger.valueOf(1)),
|
||||
ta.add(tb)
|
||||
).evalsimp();
|
||||
}
|
||||
|
||||
public Generic evalnum() {
|
||||
return ((NumericWrapper)parameter[0]).cot();
|
||||
}
|
||||
|
||||
protected Variable newinstance() {
|
||||
return new Cot(null);
|
||||
}
|
||||
}
|
82
src/misc/src/jscl/math/function/trigonometric/Sin.java
Normal file
82
src/misc/src/jscl/math/function/trigonometric/Sin.java
Normal file
@ -0,0 +1,82 @@
|
||||
package jscl.math.function.trigonometric;
|
||||
|
||||
import jscl.math.Generic;
|
||||
import jscl.math.JSCLInteger;
|
||||
import jscl.math.NotIntegrableException;
|
||||
import jscl.math.NotVariableException;
|
||||
import jscl.math.NumericWrapper;
|
||||
import jscl.math.Variable;
|
||||
import jscl.math.function.Constant;
|
||||
import jscl.math.function.Exp;
|
||||
import jscl.math.function.Trigonometric;
|
||||
|
||||
public class Sin extends Trigonometric {
|
||||
public Sin(Generic generic) {
|
||||
super("sin",new Generic[] {generic});
|
||||
}
|
||||
|
||||
public Generic antiderivative(int n) throws NotIntegrableException {
|
||||
return new Cos(parameter[0]).evaluate().negate();
|
||||
}
|
||||
|
||||
public Generic derivative(int n) {
|
||||
return new Cos(parameter[0]).evaluate();
|
||||
}
|
||||
|
||||
public Generic evaluate() {
|
||||
if(parameter[0].signum()<0) {
|
||||
return new Sin(parameter[0].negate()).evaluate().negate();
|
||||
} else if(parameter[0].signum()==0) {
|
||||
return JSCLInteger.valueOf(0);
|
||||
} else if(parameter[0].compareTo(Constant.pi)==0) {
|
||||
return JSCLInteger.valueOf(0);
|
||||
}
|
||||
return expressionValue();
|
||||
}
|
||||
|
||||
public Generic evalelem() {
|
||||
return new Exp(
|
||||
Constant.i.multiply(parameter[0])
|
||||
).evalelem().subtract(
|
||||
new Exp(
|
||||
Constant.i.multiply(parameter[0].negate())
|
||||
).evalelem()
|
||||
).multiply(Constant.i.negate().multiply(Constant.half));
|
||||
}
|
||||
|
||||
public Generic evalsimp() {
|
||||
if(parameter[0].signum()<0) {
|
||||
return new Sin(parameter[0].negate()).evaluate().negate();
|
||||
} else if(parameter[0].signum()==0) {
|
||||
return JSCLInteger.valueOf(0);
|
||||
} else if(parameter[0].compareTo(Constant.pi)==0) {
|
||||
return JSCLInteger.valueOf(0);
|
||||
}
|
||||
try {
|
||||
Variable v=parameter[0].variableValue();
|
||||
if(v instanceof Asin) {
|
||||
Generic g[]=((Asin)v).parameters();
|
||||
return g[0];
|
||||
}
|
||||
} catch (NotVariableException e) {}
|
||||
return identity();
|
||||
}
|
||||
|
||||
public Generic identity(Generic a, Generic b) {
|
||||
return new Cos(b).evalsimp().multiply(
|
||||
new Sin(a).evalsimp()
|
||||
).add(
|
||||
new Cos(a).evalsimp().multiply(
|
||||
new Sin(b).evalsimp()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public Generic evalnum() {
|
||||
return ((NumericWrapper)parameter[0]).sin();
|
||||
}
|
||||
|
||||
protected Variable newinstance() {
|
||||
return new Sin(null);
|
||||
}
|
||||
}
|
87
src/misc/src/jscl/math/function/trigonometric/Tan.java
Normal file
87
src/misc/src/jscl/math/function/trigonometric/Tan.java
Normal file
@ -0,0 +1,87 @@
|
||||
package jscl.math.function.trigonometric;
|
||||
|
||||
import jscl.math.Generic;
|
||||
import jscl.math.JSCLInteger;
|
||||
import jscl.math.NotIntegrableException;
|
||||
import jscl.math.NotVariableException;
|
||||
import jscl.math.NumericWrapper;
|
||||
import jscl.math.Variable;
|
||||
import jscl.math.function.Constant;
|
||||
import jscl.math.function.Frac;
|
||||
import jscl.math.function.Log;
|
||||
import jscl.math.function.Trigonometric;
|
||||
|
||||
public class Tan extends Trigonometric {
|
||||
public Tan(Generic generic) {
|
||||
super("tan",new Generic[] {generic});
|
||||
}
|
||||
|
||||
public Generic antiderivative(int n) throws NotIntegrableException {
|
||||
return new Log(
|
||||
JSCLInteger.valueOf(4).multiply(
|
||||
new Cos(parameter[0]).evaluate()
|
||||
)
|
||||
).evaluate().negate();
|
||||
}
|
||||
|
||||
public Generic derivative(int n) {
|
||||
return JSCLInteger.valueOf(1).add(
|
||||
new Tan(parameter[0]).evaluate().pow(2)
|
||||
);
|
||||
}
|
||||
|
||||
public Generic evaluate() {
|
||||
if(parameter[0].signum()<0) {
|
||||
return new Tan(parameter[0].negate()).evaluate().negate();
|
||||
} else if(parameter[0].signum()==0) {
|
||||
return JSCLInteger.valueOf(0);
|
||||
} else if(parameter[0].compareTo(Constant.pi)==0) {
|
||||
return JSCLInteger.valueOf(0);
|
||||
}
|
||||
return expressionValue();
|
||||
}
|
||||
|
||||
public Generic evalelem() {
|
||||
return new Frac(
|
||||
new Sin(parameter[0]).evalelem(),
|
||||
new Cos(parameter[0]).evalelem()
|
||||
).evalelem();
|
||||
}
|
||||
|
||||
public Generic evalsimp() {
|
||||
if(parameter[0].signum()<0) {
|
||||
return new Tan(parameter[0].negate()).evaluate().negate();
|
||||
} else if(parameter[0].signum()==0) {
|
||||
return JSCLInteger.valueOf(0);
|
||||
} else if(parameter[0].compareTo(Constant.pi)==0) {
|
||||
return JSCLInteger.valueOf(0);
|
||||
}
|
||||
try {
|
||||
Variable v=parameter[0].variableValue();
|
||||
if(v instanceof Atan) {
|
||||
Generic g[]=((Atan)v).parameters();
|
||||
return g[0];
|
||||
}
|
||||
} catch (NotVariableException e) {}
|
||||
return identity();
|
||||
}
|
||||
|
||||
public Generic identity(Generic a, Generic b) {
|
||||
Generic ta=new Tan(a).evalsimp();
|
||||
Generic tb=new Tan(b).evalsimp();
|
||||
return new Frac(
|
||||
ta.add(tb),
|
||||
JSCLInteger.valueOf(1).subtract(
|
||||
ta.multiply(tb)
|
||||
)
|
||||
).evalsimp();
|
||||
}
|
||||
|
||||
public Generic evalnum() {
|
||||
return ((NumericWrapper)parameter[0]).tan();
|
||||
}
|
||||
|
||||
protected Variable newinstance() {
|
||||
return new Tan(null);
|
||||
}
|
||||
}
|
182
src/misc/src/jscl/math/numeric/Complex.java
Normal file
182
src/misc/src/jscl/math/numeric/Complex.java
Normal file
@ -0,0 +1,182 @@
|
||||
package jscl.math.numeric;
|
||||
|
||||
public final class Complex extends Numeric {
|
||||
double real, imag;
|
||||
|
||||
Complex(double real, double imag) {
|
||||
this.real=real;
|
||||
this.imag=imag;
|
||||
}
|
||||
|
||||
public Complex add(Complex complex) {
|
||||
return new Complex(real+complex.real,imag+complex.imag);
|
||||
}
|
||||
|
||||
public Numeric add(Numeric numeric) {
|
||||
if(numeric instanceof Complex) {
|
||||
return add((Complex)numeric);
|
||||
} else if(numeric instanceof JSCLDouble) {
|
||||
return add(valueof(numeric));
|
||||
} else {
|
||||
return numeric.valueof(this).add(numeric);
|
||||
}
|
||||
}
|
||||
|
||||
public Complex subtract(Complex complex) {
|
||||
return new Complex(real-complex.real,imag-complex.imag);
|
||||
}
|
||||
|
||||
public Numeric subtract(Numeric numeric) {
|
||||
if(numeric instanceof Complex) {
|
||||
return subtract((Complex)numeric);
|
||||
} else if(numeric instanceof JSCLDouble) {
|
||||
return subtract(valueof(numeric));
|
||||
} else {
|
||||
return numeric.valueof(this).subtract(numeric);
|
||||
}
|
||||
}
|
||||
|
||||
public Complex multiply(Complex complex) {
|
||||
return new Complex(real*complex.real-imag*complex.imag,real*complex.imag+imag*complex.real);
|
||||
}
|
||||
|
||||
public Numeric multiply(Numeric numeric) {
|
||||
if(numeric instanceof Complex) {
|
||||
return multiply((Complex)numeric);
|
||||
} else if(numeric instanceof JSCLDouble) {
|
||||
return multiply(valueof(numeric));
|
||||
} else {
|
||||
return numeric.multiply(this);
|
||||
}
|
||||
}
|
||||
|
||||
public Complex divide(Complex complex) throws ArithmeticException {
|
||||
return multiply((Complex)complex.inverse());
|
||||
}
|
||||
|
||||
public Numeric divide(Numeric numeric) throws ArithmeticException {
|
||||
if(numeric instanceof Complex) {
|
||||
return divide((Complex)numeric);
|
||||
} else if(numeric instanceof JSCLDouble) {
|
||||
return divide(valueof(numeric));
|
||||
} else {
|
||||
return numeric.valueof(this).divide(numeric);
|
||||
}
|
||||
}
|
||||
|
||||
public Numeric negate() {
|
||||
return new Complex(-real,-imag);
|
||||
}
|
||||
|
||||
public int signum() {
|
||||
return imag==0.?(real==0.?0:(real<0.?-1:1)):(imag<0.?-1:1);
|
||||
}
|
||||
|
||||
public Complex valueof(Complex complex) {
|
||||
return new Complex(complex.real,complex.imag);
|
||||
}
|
||||
|
||||
public Numeric valueof(Numeric numeric) {
|
||||
if(numeric instanceof Complex) {
|
||||
return valueof((Complex)numeric);
|
||||
} else if(numeric instanceof JSCLDouble) {
|
||||
JSCLDouble d=(JSCLDouble)numeric;
|
||||
return new Complex(d.content,0.);
|
||||
} else throw new ArithmeticException();
|
||||
}
|
||||
|
||||
public double magnitude() {
|
||||
return Math.sqrt(real*real+imag*imag);
|
||||
}
|
||||
|
||||
public double magnitude2() {
|
||||
return real*real+imag*imag;
|
||||
}
|
||||
|
||||
public double angle() {
|
||||
return Math.atan2(imag,real);
|
||||
}
|
||||
|
||||
public Numeric log() {
|
||||
if(signum()==0) {
|
||||
return JSCLDouble.valueOf(0).log();
|
||||
} else {
|
||||
return new Complex(Math.log(magnitude()),angle());
|
||||
}
|
||||
}
|
||||
|
||||
public Numeric exp() {
|
||||
return new Complex(Math.cos(imag),Math.sin(imag)).multiply(Math.exp(real));
|
||||
}
|
||||
|
||||
public Numeric inverse() {
|
||||
return ((Complex)conjugate()).divide(magnitude2());
|
||||
}
|
||||
|
||||
Complex multiply(double d) {
|
||||
return new Complex(real*d,imag*d);
|
||||
}
|
||||
|
||||
Complex divide(double d) {
|
||||
return new Complex(real/d,imag/d);
|
||||
}
|
||||
|
||||
public Numeric conjugate() {
|
||||
return new Complex(real,-imag);
|
||||
}
|
||||
|
||||
public double realPart() {
|
||||
return real;
|
||||
}
|
||||
|
||||
public double imaginaryPart() {
|
||||
return imag;
|
||||
}
|
||||
|
||||
public int compareTo(Complex complex) {
|
||||
if(imag<complex.imag) return -1;
|
||||
else if(imag>complex.imag) return 1;
|
||||
else if(imag==complex.imag) {
|
||||
if(real<complex.real) return -1;
|
||||
else if(real>complex.real) return 1;
|
||||
else if(real==complex.real) return 0;
|
||||
else throw new ArithmeticException();
|
||||
} else throw new ArithmeticException();
|
||||
}
|
||||
|
||||
public int compareTo(Numeric numeric) {
|
||||
if(numeric instanceof Complex) {
|
||||
return compareTo((Complex)numeric);
|
||||
} else if(numeric instanceof JSCLDouble) {
|
||||
return compareTo(valueof(numeric));
|
||||
} else {
|
||||
return numeric.valueof(this).compareTo(numeric);
|
||||
}
|
||||
}
|
||||
|
||||
public static Complex valueOf(double real, double imag) {
|
||||
return new Complex(real,imag);
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer buffer=new StringBuffer();
|
||||
if(imag==0.) {
|
||||
buffer.append(real);
|
||||
} else {
|
||||
if(real==0.);
|
||||
else {
|
||||
buffer.append(real);
|
||||
if(imag<=0.);
|
||||
else buffer.append("+");
|
||||
}
|
||||
if(imag==1.);
|
||||
else if(imag==-1.) buffer.append("-");
|
||||
else {
|
||||
buffer.append(imag);
|
||||
buffer.append("*");
|
||||
}
|
||||
buffer.append("sqrt(-1)");
|
||||
}
|
||||
return buffer.toString();
|
||||
}
|
||||
}
|
174
src/misc/src/jscl/math/numeric/JSCLDouble.java
Normal file
174
src/misc/src/jscl/math/numeric/JSCLDouble.java
Normal file
@ -0,0 +1,174 @@
|
||||
package jscl.math.numeric;
|
||||
|
||||
public final class JSCLDouble extends Numeric {
|
||||
double content;
|
||||
|
||||
JSCLDouble(double val) {
|
||||
content=val;
|
||||
}
|
||||
|
||||
public JSCLDouble add(JSCLDouble dble) {
|
||||
return new JSCLDouble(content+dble.content);
|
||||
}
|
||||
|
||||
public Numeric add(Numeric numeric) {
|
||||
if(numeric instanceof JSCLDouble) {
|
||||
return add((JSCLDouble)numeric);
|
||||
} else {
|
||||
return numeric.valueof(this).add(numeric);
|
||||
}
|
||||
}
|
||||
|
||||
public JSCLDouble subtract(JSCLDouble dble) {
|
||||
return new JSCLDouble(content-dble.content);
|
||||
}
|
||||
|
||||
public Numeric subtract(Numeric numeric) {
|
||||
if(numeric instanceof JSCLDouble) {
|
||||
return subtract((JSCLDouble)numeric);
|
||||
} else {
|
||||
return numeric.valueof(this).subtract(numeric);
|
||||
}
|
||||
}
|
||||
|
||||
public JSCLDouble multiply(JSCLDouble dble) {
|
||||
return new JSCLDouble(content*dble.content);
|
||||
}
|
||||
|
||||
public Numeric multiply(Numeric numeric) {
|
||||
if(numeric instanceof JSCLDouble) {
|
||||
return multiply((JSCLDouble)numeric);
|
||||
} else {
|
||||
return numeric.multiply(this);
|
||||
}
|
||||
}
|
||||
|
||||
public JSCLDouble divide(JSCLDouble dble) throws ArithmeticException {
|
||||
return new JSCLDouble(content/dble.content);
|
||||
}
|
||||
|
||||
public Numeric divide(Numeric numeric) throws ArithmeticException {
|
||||
if(numeric instanceof JSCLDouble) {
|
||||
return divide((JSCLDouble)numeric);
|
||||
} else {
|
||||
return numeric.valueof(this).divide(numeric);
|
||||
}
|
||||
}
|
||||
|
||||
public Numeric negate() {
|
||||
return new JSCLDouble(-content);
|
||||
}
|
||||
|
||||
public int signum() {
|
||||
return content==0.?0:(content<0.?-1:1);
|
||||
}
|
||||
|
||||
public Numeric log() {
|
||||
return new JSCLDouble(Math.log(content));
|
||||
}
|
||||
|
||||
public Numeric exp() {
|
||||
return new JSCLDouble(Math.exp(content));
|
||||
}
|
||||
|
||||
public Numeric inverse() {
|
||||
return new JSCLDouble(1./content);
|
||||
}
|
||||
|
||||
public Numeric pow(JSCLDouble dble) {
|
||||
if(signum()<0) {
|
||||
return Complex.valueOf(content,0).pow(dble);
|
||||
} else {
|
||||
return new JSCLDouble(Math.pow(content,dble.content));
|
||||
}
|
||||
}
|
||||
|
||||
public Numeric pow(Numeric numeric) {
|
||||
if(numeric instanceof JSCLDouble) {
|
||||
return pow((JSCLDouble)numeric);
|
||||
} else {
|
||||
return numeric.valueof(this).pow(numeric);
|
||||
}
|
||||
}
|
||||
|
||||
public Numeric sqrt() {
|
||||
if(signum()<0) {
|
||||
return Complex.valueOf(0,1).multiply(negate().sqrt());
|
||||
} else {
|
||||
return new JSCLDouble(Math.sqrt(content));
|
||||
}
|
||||
}
|
||||
|
||||
public Numeric nthrt(int n) {
|
||||
if(signum()<0) {
|
||||
return n%2==0?sqrt().nthrt(n/2):negate().nthrt(n).negate();
|
||||
} else {
|
||||
return super.nthrt(n);
|
||||
}
|
||||
}
|
||||
|
||||
public Numeric conjugate() {
|
||||
return this;
|
||||
}
|
||||
|
||||
public Numeric acos() {
|
||||
return new JSCLDouble(Math.acos(content));
|
||||
}
|
||||
|
||||
public Numeric asin() {
|
||||
return new JSCLDouble(Math.asin(content));
|
||||
}
|
||||
|
||||
public Numeric atan() {
|
||||
return new JSCLDouble(Math.atan(content));
|
||||
}
|
||||
|
||||
public Numeric cos() {
|
||||
return new JSCLDouble(Math.cos(content));
|
||||
}
|
||||
|
||||
public Numeric sin() {
|
||||
return new JSCLDouble(Math.sin(content));
|
||||
}
|
||||
|
||||
public Numeric tan() {
|
||||
return new JSCLDouble(Math.tan(content));
|
||||
}
|
||||
|
||||
public JSCLDouble valueof(JSCLDouble dble) {
|
||||
return new JSCLDouble(dble.content);
|
||||
}
|
||||
|
||||
public Numeric valueof(Numeric numeric) {
|
||||
if(numeric instanceof JSCLDouble) {
|
||||
return valueof((JSCLDouble)numeric);
|
||||
} else throw new ArithmeticException();
|
||||
}
|
||||
|
||||
public double doubleValue() {
|
||||
return content;
|
||||
}
|
||||
|
||||
public int compareTo(JSCLDouble dble) {
|
||||
if(content<dble.content) return -1;
|
||||
else if(content>dble.content) return 1;
|
||||
else if(content==dble.content) return 0;
|
||||
else throw new ArithmeticException();
|
||||
}
|
||||
|
||||
public int compareTo(Numeric numeric) {
|
||||
if(numeric instanceof JSCLDouble) {
|
||||
return compareTo((JSCLDouble)numeric);
|
||||
} else {
|
||||
return numeric.valueof(this).compareTo(numeric);
|
||||
}
|
||||
}
|
||||
|
||||
public static JSCLDouble valueOf(double val) {
|
||||
return new JSCLDouble(val);
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return new Double(content).toString();
|
||||
}
|
||||
}
|
156
src/misc/src/jscl/math/numeric/Numeric.java
Normal file
156
src/misc/src/jscl/math/numeric/Numeric.java
Normal file
@ -0,0 +1,156 @@
|
||||
package jscl.math.numeric;
|
||||
|
||||
import jscl.math.Arithmetic;
|
||||
|
||||
public abstract class Numeric implements Arithmetic, Comparable {
|
||||
public abstract Numeric add(Numeric numeric);
|
||||
|
||||
public Numeric subtract(Numeric numeric) {
|
||||
return add(numeric.negate());
|
||||
}
|
||||
|
||||
public abstract Numeric multiply(Numeric numeric);
|
||||
public abstract Numeric divide(Numeric numeric) throws ArithmeticException;
|
||||
|
||||
public Arithmetic add(Arithmetic arithmetic) {
|
||||
return add((Numeric)arithmetic);
|
||||
}
|
||||
|
||||
public Arithmetic subtract(Arithmetic arithmetic) {
|
||||
return subtract((Numeric)arithmetic);
|
||||
}
|
||||
|
||||
public Arithmetic multiply(Arithmetic arithmetic) {
|
||||
return multiply((Numeric)arithmetic);
|
||||
}
|
||||
|
||||
public Arithmetic divide(Arithmetic arithmetic) throws ArithmeticException {
|
||||
return divide((Numeric)arithmetic);
|
||||
}
|
||||
|
||||
public Numeric pow(int exponent) {
|
||||
Numeric a=JSCLDouble.valueOf(1);
|
||||
for(int i=0;i<exponent;i++) a=a.multiply(this);
|
||||
return a;
|
||||
}
|
||||
|
||||
public Numeric abs() {
|
||||
return signum()<0?negate():(signum()==0?JSCLDouble.valueOf(1):this);
|
||||
}
|
||||
|
||||
public abstract Numeric negate();
|
||||
public abstract int signum();
|
||||
|
||||
public Numeric sgn() {
|
||||
return divide(abs());
|
||||
}
|
||||
|
||||
public abstract Numeric log();
|
||||
public abstract Numeric exp();
|
||||
|
||||
public Numeric inverse() {
|
||||
return JSCLDouble.valueOf(1).divide(this);
|
||||
}
|
||||
|
||||
public Numeric pow(Numeric numeric) {
|
||||
if(numeric.signum()==0) {
|
||||
return JSCLDouble.valueOf(1);
|
||||
} else if(numeric.compareTo(JSCLDouble.valueOf(1))==0) {
|
||||
return this;
|
||||
} else {
|
||||
return numeric.multiply(log()).exp();
|
||||
}
|
||||
}
|
||||
|
||||
public Numeric sqrt() {
|
||||
return nthrt(2);
|
||||
}
|
||||
|
||||
public Numeric nthrt(int n) {
|
||||
return pow(JSCLDouble.valueOf(1./n));
|
||||
}
|
||||
|
||||
public static Numeric root(int subscript, Numeric parameter[]) {
|
||||
throw new ArithmeticException();
|
||||
}
|
||||
|
||||
public abstract Numeric conjugate();
|
||||
|
||||
public Numeric acos() {
|
||||
return add(JSCLDouble.valueOf(-1).add(pow(2)).sqrt()).log().multiply(Complex.valueOf(0, 1));
|
||||
}
|
||||
|
||||
public Numeric asin() {
|
||||
return multiply(Complex.valueOf(0, 1)).negate().add(JSCLDouble.valueOf(1).subtract(pow(2)).sqrt()).log().multiply(Complex.valueOf(0, 1));
|
||||
}
|
||||
|
||||
public Numeric atan() {
|
||||
return Complex.valueOf(0, 1).multiply(Complex.valueOf(0, 1).add(this).divide(Complex.valueOf(0, 1).subtract(this)).log()).divide(JSCLDouble.valueOf(2));
|
||||
}
|
||||
|
||||
public Numeric acot() {
|
||||
return Complex.valueOf(0, 1).multiply(Complex.valueOf(0, 1).add(this).divide(Complex.valueOf(0, 1).subtract(this)).negate().log()).divide(JSCLDouble.valueOf(2));
|
||||
}
|
||||
|
||||
public Numeric cos() {
|
||||
return JSCLDouble.valueOf(1).add(multiply(Complex.valueOf(0, 1)).exp().pow(2)).divide(JSCLDouble.valueOf(2).multiply(multiply(Complex.valueOf(0, 1)).exp()));
|
||||
}
|
||||
|
||||
public Numeric sin() {
|
||||
return Complex.valueOf(0, 1).subtract(multiply(Complex.valueOf(0, 1)).exp().pow(2).multiply(Complex.valueOf(0, 1))).divide(JSCLDouble.valueOf(2).multiply(multiply(Complex.valueOf(0, 1)).exp()));
|
||||
}
|
||||
|
||||
public Numeric tan() {
|
||||
return Complex.valueOf(0, 1).subtract(multiply(Complex.valueOf(0, 1)).exp().pow(2).multiply(Complex.valueOf(0, 1))).divide(JSCLDouble.valueOf(1).add(multiply(Complex.valueOf(0, 1)).exp().pow(2)));
|
||||
}
|
||||
|
||||
public Numeric cot() {
|
||||
return Complex.valueOf(0, 1).add(Complex.valueOf(0, 1).multiply(Complex.valueOf(0, 1).multiply(this).exp().pow(2))).divide(JSCLDouble.valueOf(1).subtract(Complex.valueOf(0, 1).multiply(this).exp().pow(2))).negate();
|
||||
}
|
||||
|
||||
public Numeric acosh() {
|
||||
return add(JSCLDouble.valueOf(-1).add(pow(2)).sqrt()).log();
|
||||
}
|
||||
|
||||
public Numeric asinh() {
|
||||
return add(JSCLDouble.valueOf(1).add(pow(2)).sqrt()).log();
|
||||
}
|
||||
|
||||
public Numeric atanh() {
|
||||
return JSCLDouble.valueOf(1).add(this).divide(JSCLDouble.valueOf(1).subtract(this)).log().divide(JSCLDouble.valueOf(2));
|
||||
}
|
||||
|
||||
public Numeric acoth() {
|
||||
return JSCLDouble.valueOf(1).add(this).divide(JSCLDouble.valueOf(1).subtract(this)).negate().log().divide(JSCLDouble.valueOf(2));
|
||||
}
|
||||
|
||||
public Numeric cosh() {
|
||||
return JSCLDouble.valueOf(1).add(exp().pow(2)).divide(JSCLDouble.valueOf(2).multiply(exp()));
|
||||
}
|
||||
|
||||
public Numeric sinh() {
|
||||
return JSCLDouble.valueOf(1).subtract(exp().pow(2)).divide(JSCLDouble.valueOf(2).multiply(exp())).negate();
|
||||
}
|
||||
|
||||
public Numeric tanh() {
|
||||
return JSCLDouble.valueOf(1).subtract(exp().pow(2)).divide(JSCLDouble.valueOf(1).add(exp().pow(2))).negate();
|
||||
}
|
||||
|
||||
public Numeric coth() {
|
||||
return JSCLDouble.valueOf(1).add(exp().pow(2)).divide(JSCLDouble.valueOf(1).subtract(exp().pow(2))).negate();
|
||||
}
|
||||
|
||||
public abstract Numeric valueof(Numeric numeric);
|
||||
|
||||
public abstract int compareTo(Numeric numeric);
|
||||
|
||||
public int compareTo(Object o) {
|
||||
return compareTo((Numeric)o);
|
||||
}
|
||||
|
||||
public boolean equals(Object obj) {
|
||||
if(obj instanceof Numeric) {
|
||||
return compareTo((Numeric)obj)==0;
|
||||
} else return false;
|
||||
}
|
||||
}
|
277
src/misc/src/jscl/math/numeric/NumericMatrix.java
Normal file
277
src/misc/src/jscl/math/numeric/NumericMatrix.java
Normal file
@ -0,0 +1,277 @@
|
||||
package jscl.math.numeric;
|
||||
|
||||
import jscl.util.ArrayComparator;
|
||||
|
||||
public class NumericMatrix extends Numeric {
|
||||
protected final Numeric element[][];
|
||||
protected final int n,p;
|
||||
|
||||
public NumericMatrix(Numeric element[][]) {
|
||||
this.element=element;
|
||||
n=element.length;
|
||||
p=element.length>0?element[0].length:0;
|
||||
}
|
||||
|
||||
public Numeric[][] elements() {
|
||||
return element;
|
||||
}
|
||||
|
||||
public NumericMatrix add(NumericMatrix matrix) {
|
||||
NumericMatrix m=(NumericMatrix)newinstance();
|
||||
for(int i=0;i<n;i++) {
|
||||
for(int j=0;j<p;j++) {
|
||||
m.element[i][j]=element[i][j].add(matrix.element[i][j]);
|
||||
}
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
public Numeric add(Numeric numeric) {
|
||||
if(numeric instanceof NumericMatrix) {
|
||||
return add((NumericMatrix)numeric);
|
||||
} else {
|
||||
return add(valueof(numeric));
|
||||
}
|
||||
}
|
||||
|
||||
public NumericMatrix subtract(NumericMatrix matrix) {
|
||||
NumericMatrix m=(NumericMatrix)newinstance();
|
||||
for(int i=0;i<n;i++) {
|
||||
for(int j=0;j<p;j++) {
|
||||
m.element[i][j]=element[i][j].subtract(matrix.element[i][j]);
|
||||
}
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
public Numeric subtract(Numeric numeric) {
|
||||
if(numeric instanceof NumericMatrix) {
|
||||
return subtract((NumericMatrix)numeric);
|
||||
} else {
|
||||
return subtract(valueof(numeric));
|
||||
}
|
||||
}
|
||||
|
||||
public NumericMatrix multiply(NumericMatrix matrix) {
|
||||
if(p!=matrix.n) throw new ArithmeticException();
|
||||
NumericMatrix m=(NumericMatrix)newinstance(new Numeric[n][matrix.p]);
|
||||
for(int i=0;i<n;i++) {
|
||||
for(int j=0;j<matrix.p;j++) {
|
||||
m.element[i][j]=JSCLDouble.valueOf(0);
|
||||
for(int k=0;k<p;k++) {
|
||||
m.element[i][j]=m.element[i][j].add(element[i][k].multiply(matrix.element[k][j]));
|
||||
}
|
||||
}
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
public Numeric multiply(Numeric numeric) {
|
||||
if(numeric instanceof NumericMatrix) {
|
||||
return multiply((NumericMatrix)numeric);
|
||||
} else if(numeric instanceof NumericVector) {
|
||||
NumericVector v=(NumericVector)((NumericVector)numeric).newinstance(new Numeric[n]);
|
||||
NumericVector v2=(NumericVector)numeric;
|
||||
if(p!=v2.n) throw new ArithmeticException();
|
||||
for(int i=0;i<n;i++) {
|
||||
v.element[i]=JSCLDouble.valueOf(0);
|
||||
for(int k=0;k<p;k++) {
|
||||
v.element[i]=v.element[i].add(element[i][k].multiply(v2.element[k]));
|
||||
}
|
||||
}
|
||||
return v;
|
||||
} else {
|
||||
NumericMatrix m=(NumericMatrix)newinstance();
|
||||
for(int i=0;i<n;i++) {
|
||||
for(int j=0;j<p;j++) {
|
||||
m.element[i][j]=element[i][j].multiply((Numeric)numeric);
|
||||
}
|
||||
}
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
public Numeric divide(Numeric numeric) throws ArithmeticException {
|
||||
if(numeric instanceof NumericMatrix) {
|
||||
return multiply(((NumericMatrix)numeric).inverse());
|
||||
} else if(numeric instanceof NumericVector) {
|
||||
throw new ArithmeticException();
|
||||
} else {
|
||||
NumericMatrix m=(NumericMatrix)newinstance();
|
||||
for(int i=0;i<n;i++) {
|
||||
for(int j=0;j<p;j++) {
|
||||
m.element[i][j]=element[i][j].divide((Numeric)numeric);
|
||||
}
|
||||
}
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
public Numeric negate() {
|
||||
NumericMatrix m=(NumericMatrix)newinstance();
|
||||
for(int i=0;i<n;i++) {
|
||||
for(int j=0;j<p;j++) {
|
||||
m.element[i][j]=element[i][j].negate();
|
||||
}
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
public int signum() {
|
||||
for(int i=0;i<n;i++) {
|
||||
for(int j=0;j<p;j++) {
|
||||
int c=element[i][j].signum();
|
||||
if(c<0) return -1;
|
||||
else if(c>0) return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public Numeric valueof(Numeric numeric) {
|
||||
if(numeric instanceof NumericMatrix || numeric instanceof NumericVector) {
|
||||
throw new ArithmeticException();
|
||||
} else {
|
||||
NumericMatrix m=(NumericMatrix)identity(n,p).multiply(numeric);
|
||||
return newinstance(m.element);
|
||||
}
|
||||
}
|
||||
|
||||
public Numeric[] vectors() {
|
||||
NumericVector v[]=new NumericVector[n];
|
||||
for(int i=0;i<n;i++) {
|
||||
v[i]=new NumericVector(new Numeric[p]);
|
||||
for(int j=0;j<p;j++) {
|
||||
v[i].element[j]=element[i][j];
|
||||
}
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
public Numeric transpose() {
|
||||
NumericMatrix m=(NumericMatrix)newinstance(new Numeric[p][n]);
|
||||
for(int i=0;i<n;i++) {
|
||||
for(int j=0;j<p;j++) {
|
||||
m.element[j][i]=element[i][j];
|
||||
}
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
public Numeric trace() {
|
||||
Numeric s=JSCLDouble.valueOf(0);
|
||||
for(int i=0;i<n;i++) {
|
||||
s=s.add(element[i][i]);
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
public Numeric inverse() {
|
||||
NumericMatrix m=(NumericMatrix)newinstance();
|
||||
for(int i=0;i<n;i++) {
|
||||
for(int j=0;j<n;j++) {
|
||||
m.element[i][j]=inverseElement(i,j);
|
||||
}
|
||||
}
|
||||
return m.transpose().divide(determinant());
|
||||
}
|
||||
|
||||
Numeric inverseElement(int k, int l) {
|
||||
NumericMatrix m=(NumericMatrix)newinstance();
|
||||
for(int i=0;i<n;i++) {
|
||||
for(int j=0;j<n;j++) {
|
||||
m.element[i][j]=i==k?JSCLDouble.valueOf(j==l?1:0):element[i][j];
|
||||
}
|
||||
}
|
||||
return m.determinant();
|
||||
}
|
||||
|
||||
public Numeric determinant() {
|
||||
if(n>1) {
|
||||
Numeric a=JSCLDouble.valueOf(0);
|
||||
for(int i=0;i<n;i++) {
|
||||
if(element[i][0].signum()==0);
|
||||
else {
|
||||
NumericMatrix m=(NumericMatrix)newinstance(new Numeric[n-1][n-1]);
|
||||
for(int j=0;j<n-1;j++) {
|
||||
for(int k=0;k<n-1;k++) m.element[j][k]=element[j<i?j:j+1][k+1];
|
||||
}
|
||||
if(i%2==0) a=a.add(element[i][0].multiply(m.determinant()));
|
||||
else a=a.subtract(element[i][0].multiply(m.determinant()));
|
||||
}
|
||||
}
|
||||
return a;
|
||||
} else if(n>0) return element[0][0];
|
||||
else return JSCLDouble.valueOf(0);
|
||||
}
|
||||
|
||||
public Numeric log() {
|
||||
throw new ArithmeticException();
|
||||
}
|
||||
|
||||
public Numeric exp() {
|
||||
throw new ArithmeticException();
|
||||
}
|
||||
|
||||
public Numeric conjugate() {
|
||||
NumericMatrix m=(NumericMatrix)newinstance();
|
||||
for(int i=0;i<n;i++) {
|
||||
for(int j=0;j<p;j++) {
|
||||
m.element[i][j]=element[i][j].conjugate();
|
||||
}
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
public int compareTo(NumericMatrix matrix) {
|
||||
return ArrayComparator.comparator.compare(vectors(),matrix.vectors());
|
||||
}
|
||||
|
||||
public int compareTo(Numeric numeric) {
|
||||
if(numeric instanceof NumericMatrix) {
|
||||
return compareTo((NumericMatrix)numeric);
|
||||
} else {
|
||||
return compareTo(valueof(numeric));
|
||||
}
|
||||
}
|
||||
|
||||
public static NumericMatrix identity(int dimension) {
|
||||
return identity(dimension,dimension);
|
||||
}
|
||||
|
||||
public static NumericMatrix identity(int n, int p) {
|
||||
NumericMatrix m=new NumericMatrix(new Numeric[n][p]);
|
||||
for(int i=0;i<n;i++) {
|
||||
for(int j=0;j<p;j++) {
|
||||
if(i==j) {
|
||||
m.element[i][j]=JSCLDouble.valueOf(1);
|
||||
} else {
|
||||
m.element[i][j]=JSCLDouble.valueOf(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer buffer=new StringBuffer();
|
||||
buffer.append("{");
|
||||
for(int i=0;i<n;i++) {
|
||||
buffer.append("{");
|
||||
for(int j=0;j<p;j++) {
|
||||
buffer.append(element[i][j]).append(j<p-1?", ":"");
|
||||
}
|
||||
buffer.append("}").append(i<n-1?",\n":"");
|
||||
}
|
||||
buffer.append("}");
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
protected NumericMatrix newinstance() {
|
||||
return newinstance(new Numeric[n][p]);
|
||||
}
|
||||
|
||||
protected NumericMatrix newinstance(Numeric element[][]) {
|
||||
return new NumericMatrix(element);
|
||||
}
|
||||
}
|
160
src/misc/src/jscl/math/numeric/NumericVector.java
Normal file
160
src/misc/src/jscl/math/numeric/NumericVector.java
Normal file
@ -0,0 +1,160 @@
|
||||
package jscl.math.numeric;
|
||||
|
||||
import jscl.util.ArrayComparator;
|
||||
|
||||
public class NumericVector extends Numeric {
|
||||
protected final Numeric element[];
|
||||
protected final int n;
|
||||
|
||||
public NumericVector(Numeric element[]) {
|
||||
this.element=element;
|
||||
n=element.length;
|
||||
}
|
||||
|
||||
public Numeric[] elements() {
|
||||
return element;
|
||||
}
|
||||
|
||||
public NumericVector add(NumericVector vector) {
|
||||
NumericVector v=(NumericVector)newinstance();
|
||||
for(int i=0;i<n;i++) v.element[i]=element[i].add(vector.element[i]);
|
||||
return v;
|
||||
}
|
||||
|
||||
public Numeric add(Numeric numeric) {
|
||||
if(numeric instanceof NumericVector) {
|
||||
return add((NumericVector)numeric);
|
||||
} else {
|
||||
return add(valueof(numeric));
|
||||
}
|
||||
}
|
||||
|
||||
public NumericVector subtract(NumericVector vector) {
|
||||
NumericVector v=(NumericVector)newinstance();
|
||||
for(int i=0;i<n;i++) v.element[i]=element[i].subtract(vector.element[i]);
|
||||
return v;
|
||||
}
|
||||
|
||||
public Numeric subtract(Numeric numeric) {
|
||||
if(numeric instanceof NumericVector) {
|
||||
return subtract((NumericVector)numeric);
|
||||
} else {
|
||||
return subtract(valueof(numeric));
|
||||
}
|
||||
}
|
||||
|
||||
public Numeric multiply(Numeric numeric) {
|
||||
if(numeric instanceof NumericVector) {
|
||||
return scalarProduct((NumericVector)numeric);
|
||||
} else if(numeric instanceof NumericMatrix) {
|
||||
return ((NumericMatrix)numeric).transpose().multiply(this);
|
||||
} else {
|
||||
NumericVector v=(NumericVector)newinstance();
|
||||
for(int i=0;i<n;i++) v.element[i]=element[i].multiply((Numeric)numeric);
|
||||
return v;
|
||||
}
|
||||
}
|
||||
|
||||
public Numeric divide(Numeric numeric) throws ArithmeticException {
|
||||
if(numeric instanceof NumericVector) {
|
||||
throw new ArithmeticException();
|
||||
} else if(numeric instanceof NumericMatrix) {
|
||||
return multiply(((NumericMatrix)numeric).inverse());
|
||||
} else {
|
||||
NumericVector v=(NumericVector)newinstance();
|
||||
for(int i=0;i<n;i++) {
|
||||
v.element[i]=element[i].divide((Numeric)numeric);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
}
|
||||
|
||||
public Numeric negate() {
|
||||
NumericVector v=(NumericVector)newinstance();
|
||||
for(int i=0;i<n;i++) v.element[i]=element[i].negate();
|
||||
return v;
|
||||
}
|
||||
|
||||
public int signum() {
|
||||
for(int i=0;i<n;i++) {
|
||||
int c=element[i].signum();
|
||||
if(c<0) return -1;
|
||||
else if(c>0) return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public Numeric valueof(Numeric numeric) {
|
||||
if(numeric instanceof NumericVector || numeric instanceof NumericMatrix) {
|
||||
throw new ArithmeticException();
|
||||
} else {
|
||||
NumericVector v=(NumericVector)unity(n).multiply(numeric);
|
||||
return newinstance(v.element);
|
||||
}
|
||||
}
|
||||
|
||||
public Numeric magnitude2() {
|
||||
return scalarProduct(this);
|
||||
}
|
||||
|
||||
public Numeric scalarProduct(NumericVector vector) {
|
||||
Numeric a=JSCLDouble.valueOf(0);
|
||||
for(int i=0;i<n;i++) {
|
||||
a=a.add(element[i].multiply(vector.element[i]));
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
public Numeric log() {
|
||||
throw new ArithmeticException();
|
||||
}
|
||||
|
||||
public Numeric exp() {
|
||||
throw new ArithmeticException();
|
||||
}
|
||||
|
||||
public Numeric conjugate() {
|
||||
NumericVector v=(NumericVector)newinstance();
|
||||
for(int i=0;i<n;i++) v.element[i]=element[i].conjugate();
|
||||
return v;
|
||||
}
|
||||
|
||||
public int compareTo(NumericVector vector) {
|
||||
return ArrayComparator.comparator.compare(element,vector.element);
|
||||
}
|
||||
|
||||
public int compareTo(Numeric numeric) {
|
||||
if(numeric instanceof NumericVector) {
|
||||
return compareTo((NumericVector)numeric);
|
||||
} else {
|
||||
return compareTo(valueof(numeric));
|
||||
}
|
||||
}
|
||||
|
||||
public static NumericVector unity(int dimension) {
|
||||
NumericVector v=new NumericVector(new Numeric[dimension]);
|
||||
for(int i=0;i<v.n;i++) {
|
||||
if(i==0) v.element[i]=JSCLDouble.valueOf(1);
|
||||
else v.element[i]=JSCLDouble.valueOf(0);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer buffer=new StringBuffer();
|
||||
buffer.append("{");
|
||||
for(int i=0;i<n;i++) {
|
||||
buffer.append(element[i]).append(i<n-1?", ":"");
|
||||
}
|
||||
buffer.append("}");
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
protected NumericVector newinstance() {
|
||||
return newinstance(new Numeric[n]);
|
||||
}
|
||||
|
||||
protected NumericVector newinstance(Numeric element[]) {
|
||||
return new NumericVector(element);
|
||||
}
|
||||
}
|
24
src/misc/src/jscl/math/operator/Coefficient.java
Normal file
24
src/misc/src/jscl/math/operator/Coefficient.java
Normal file
@ -0,0 +1,24 @@
|
||||
package jscl.math.operator;
|
||||
|
||||
import jscl.math.Generic;
|
||||
import jscl.math.JSCLVector;
|
||||
import jscl.math.Variable;
|
||||
import jscl.math.polynomial.Polynomial;
|
||||
|
||||
public class Coefficient extends Operator {
|
||||
public Coefficient(Generic expression, Generic variable) {
|
||||
super("coef",new Generic[] {expression,variable});
|
||||
}
|
||||
|
||||
public Generic compute() {
|
||||
Variable variable=parameter[1].variableValue();
|
||||
if(parameter[0].isPolynomial(variable)) {
|
||||
return new JSCLVector(Polynomial.factory(variable).valueof(parameter[0]).elements());
|
||||
}
|
||||
return expressionValue();
|
||||
}
|
||||
|
||||
protected Variable newinstance() {
|
||||
return new Coefficient(null,null);
|
||||
}
|
||||
}
|
112
src/misc/src/jscl/math/operator/Derivative.java
Normal file
112
src/misc/src/jscl/math/operator/Derivative.java
Normal file
@ -0,0 +1,112 @@
|
||||
package jscl.math.operator;
|
||||
|
||||
import jscl.math.Generic;
|
||||
import jscl.math.JSCLInteger;
|
||||
import jscl.math.NotIntegerException;
|
||||
import jscl.math.Variable;
|
||||
import jscl.mathml.MathML;
|
||||
|
||||
public class Derivative extends Operator {
|
||||
public Derivative(Generic expression, Generic variable, Generic value, Generic order) {
|
||||
super("d",new Generic[] {expression,variable,value,order});
|
||||
}
|
||||
|
||||
public Generic compute() {
|
||||
Variable variable=parameter[1].variableValue();
|
||||
try {
|
||||
int n=parameter[3].integerValue().intValue();
|
||||
Generic a=parameter[0];
|
||||
for(int i=0;i<n;i++) {
|
||||
a=a.derivative(variable);
|
||||
}
|
||||
return a.substitute(variable,parameter[2]);
|
||||
} catch (NotIntegerException e) {}
|
||||
return expressionValue();
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer buffer=new StringBuffer();
|
||||
int n=4;
|
||||
if(parameter[3].compareTo(JSCLInteger.valueOf(1))==0) {
|
||||
n=3;
|
||||
if(parameter[2].compareTo(parameter[1])==0) n=2;
|
||||
}
|
||||
buffer.append(name);
|
||||
buffer.append("(");
|
||||
for(int i=0;i<n;i++) {
|
||||
buffer.append(parameter[i]).append(i<n-1?", ":"");
|
||||
}
|
||||
buffer.append(")");
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
public void toMathML(MathML element, Object data) {
|
||||
int exponent=data instanceof Integer?((Integer)data).intValue():1;
|
||||
if(exponent==1) derivationToMathML(element,false);
|
||||
else {
|
||||
MathML e1=element.element("msup");
|
||||
derivationToMathML(e1,true);
|
||||
MathML e2=element.element("mn");
|
||||
e2.appendChild(element.text(String.valueOf(exponent)));
|
||||
e1.appendChild(e2);
|
||||
element.appendChild(e1);
|
||||
}
|
||||
MathML e1=element.element("mfenced");
|
||||
parameter[0].toMathML(e1,null);
|
||||
if(parameter[2].compareTo(parameter[1])!=0) parameter[2].toMathML(e1,null);
|
||||
element.appendChild(e1);
|
||||
}
|
||||
|
||||
void derivationToMathML(MathML element, boolean fenced) {
|
||||
if(fenced) {
|
||||
MathML e1=element.element("mfenced");
|
||||
derivationToMathML(e1);
|
||||
element.appendChild(e1);
|
||||
} else {
|
||||
derivationToMathML(element);
|
||||
}
|
||||
}
|
||||
|
||||
void derivationToMathML(MathML element) {
|
||||
Variable v=parameter[1].variableValue();
|
||||
int n=0;
|
||||
try {
|
||||
n=parameter[3].integerValue().intValue();
|
||||
} catch (NotIntegerException e) {}
|
||||
if(n==1) {
|
||||
MathML e1=element.element("mfrac");
|
||||
MathML e2=element.element("mo");
|
||||
e2.appendChild(element.text(/*"\u2146"*/"d"));
|
||||
e1.appendChild(e2);
|
||||
e2=element.element("mrow");
|
||||
MathML e3=element.element("mo");
|
||||
e3.appendChild(element.text(/*"\u2146"*/"d"));
|
||||
e2.appendChild(e3);
|
||||
v.toMathML(e2,null);
|
||||
e1.appendChild(e2);
|
||||
element.appendChild(e1);
|
||||
} else {
|
||||
MathML e1=element.element("mfrac");
|
||||
MathML e2=element.element("msup");
|
||||
MathML e3=element.element("mo");
|
||||
e3.appendChild(element.text(/*"\u2146"*/"d"));
|
||||
e2.appendChild(e3);
|
||||
parameter[3].toMathML(e2,null);
|
||||
e1.appendChild(e2);
|
||||
e2=element.element("mrow");
|
||||
e3=element.element("mo");
|
||||
e3.appendChild(element.text(/*"\u2146"*/"d"));
|
||||
e2.appendChild(e3);
|
||||
e3=element.element("msup");
|
||||
parameter[1].toMathML(e3,null);
|
||||
parameter[3].toMathML(e3,null);
|
||||
e2.appendChild(e3);
|
||||
e1.appendChild(e2);
|
||||
element.appendChild(e1);
|
||||
}
|
||||
}
|
||||
|
||||
protected Variable newinstance() {
|
||||
return new Derivative(null,null,null,null);
|
||||
}
|
||||
}
|
18
src/misc/src/jscl/math/operator/Division.java
Normal file
18
src/misc/src/jscl/math/operator/Division.java
Normal file
@ -0,0 +1,18 @@
|
||||
package jscl.math.operator;
|
||||
|
||||
import jscl.math.Generic;
|
||||
import jscl.math.Variable;
|
||||
|
||||
public class Division extends Operator {
|
||||
public Division(Generic expression1, Generic expression2) {
|
||||
super("div",new Generic[] {expression1,expression2});
|
||||
}
|
||||
|
||||
public Generic compute() {
|
||||
return parameter[0].divideAndRemainder(parameter[1])[0];
|
||||
}
|
||||
|
||||
protected Variable newinstance() {
|
||||
return new Division(null,null);
|
||||
}
|
||||
}
|
98
src/misc/src/jscl/math/operator/Factorial.java
Normal file
98
src/misc/src/jscl/math/operator/Factorial.java
Normal file
@ -0,0 +1,98 @@
|
||||
package jscl.math.operator;
|
||||
|
||||
import jscl.math.Generic;
|
||||
import jscl.math.GenericVariable;
|
||||
import jscl.math.JSCLInteger;
|
||||
import jscl.math.NotIntegerException;
|
||||
import jscl.math.NotVariableException;
|
||||
import jscl.math.Variable;
|
||||
import jscl.math.function.Frac;
|
||||
import jscl.math.function.Pow;
|
||||
import jscl.mathml.MathML;
|
||||
import jscl.util.ArrayComparator;
|
||||
|
||||
public class Factorial extends Operator {
|
||||
public Factorial(Generic expression) {
|
||||
super("",new Generic[] {expression});
|
||||
}
|
||||
|
||||
public Generic compute() {
|
||||
try {
|
||||
int n=parameter[0].integerValue().intValue();
|
||||
Generic a=JSCLInteger.valueOf(1);
|
||||
for(int i=0;i<n;i++) {
|
||||
a=a.multiply(JSCLInteger.valueOf(i+1));
|
||||
}
|
||||
return a;
|
||||
} catch (NotIntegerException e) {}
|
||||
return expressionValue();
|
||||
}
|
||||
|
||||
public int compareTo(Variable variable) {
|
||||
if(this==variable) return 0;
|
||||
int c=comparator.compare(this,variable);
|
||||
if(c<0) return -1;
|
||||
else if(c>0) return 1;
|
||||
else {
|
||||
Factorial v=(Factorial)variable;
|
||||
return ArrayComparator.comparator.compare(parameter,v.parameter);
|
||||
}
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer buffer=new StringBuffer();
|
||||
try {
|
||||
JSCLInteger en=parameter[0].integerValue();
|
||||
buffer.append(en);
|
||||
} catch (NotIntegerException e) {
|
||||
try {
|
||||
Variable v=parameter[0].variableValue();
|
||||
if(v instanceof Frac || v instanceof Pow) {
|
||||
buffer.append(GenericVariable.valueOf(parameter[0]));
|
||||
} else buffer.append(v);
|
||||
} catch (NotVariableException e2) {
|
||||
buffer.append(GenericVariable.valueOf(parameter[0]));
|
||||
}
|
||||
}
|
||||
buffer.append("!");
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
public void toMathML(MathML element, Object data) {
|
||||
int exponent=data instanceof Integer?((Integer)data).intValue():1;
|
||||
if(exponent==1) bodyToMathML(element);
|
||||
else {
|
||||
MathML e1=element.element("msup");
|
||||
bodyToMathML(e1);
|
||||
MathML e2=element.element("mn");
|
||||
e2.appendChild(element.text(String.valueOf(exponent)));
|
||||
e1.appendChild(e2);
|
||||
element.appendChild(e1);
|
||||
}
|
||||
}
|
||||
|
||||
void bodyToMathML(MathML element) {
|
||||
MathML e1=element.element("mrow");
|
||||
try {
|
||||
JSCLInteger en=parameter[0].integerValue();
|
||||
en.toMathML(e1,null);
|
||||
} catch (NotIntegerException e) {
|
||||
try {
|
||||
Variable v=parameter[0].variableValue();
|
||||
if(v instanceof Pow) {
|
||||
GenericVariable.valueOf(parameter[0]).toMathML(e1,null);
|
||||
} else v.toMathML(e1,null);
|
||||
} catch (NotVariableException e2) {
|
||||
GenericVariable.valueOf(parameter[0]).toMathML(e1,null);
|
||||
}
|
||||
}
|
||||
MathML e2=element.element("mo");
|
||||
e2.appendChild(element.text("!"));
|
||||
e1.appendChild(e2);
|
||||
element.appendChild(e1);
|
||||
}
|
||||
|
||||
protected Variable newinstance() {
|
||||
return new Factorial(null);
|
||||
}
|
||||
}
|
138
src/misc/src/jscl/math/operator/Groebner.java
Normal file
138
src/misc/src/jscl/math/operator/Groebner.java
Normal file
@ -0,0 +1,138 @@
|
||||
package jscl.math.operator;
|
||||
|
||||
import jscl.math.Generic;
|
||||
import jscl.math.GenericVariable;
|
||||
import jscl.math.JSCLInteger;
|
||||
import jscl.math.JSCLVector;
|
||||
import jscl.math.Variable;
|
||||
import jscl.math.function.Constant;
|
||||
import jscl.math.function.ImplicitFunction;
|
||||
import jscl.math.polynomial.Basis;
|
||||
import jscl.math.polynomial.Monomial;
|
||||
import jscl.math.polynomial.Ordering;
|
||||
import jscl.math.polynomial.Polynomial;
|
||||
import jscl.mathml.MathML;
|
||||
|
||||
public class Groebner extends Operator {
|
||||
public Groebner(Generic generic, Generic variable, Generic ordering, Generic modulo) {
|
||||
super("groebner",new Generic[] {generic,variable,ordering,modulo});
|
||||
}
|
||||
|
||||
public Generic compute() {
|
||||
Generic generic[]=((JSCLVector)parameter[0]).elements();
|
||||
Variable variable[]=variables(parameter[1]);
|
||||
Ordering ord=ordering(parameter[2]);
|
||||
int m=parameter[3].integerValue().intValue();
|
||||
return new PolynomialVector(Basis.compute(generic,variable,ord,m));
|
||||
}
|
||||
|
||||
public Operator transmute() {
|
||||
Generic p[]=new Generic[] {GenericVariable.content(parameter[0]),GenericVariable.content(parameter[1])};
|
||||
if(p[0] instanceof JSCLVector && p[1] instanceof JSCLVector) {
|
||||
Generic generic[]=((JSCLVector)p[0]).elements();
|
||||
Variable variable[]=variables(p[1]);
|
||||
Ordering ord=ordering(parameter[2]);
|
||||
int m=parameter[3].integerValue().intValue();
|
||||
return new Groebner(new PolynomialVector(new Basis(generic,Polynomial.factory(variable,ord,m))),p[1],parameter[2],parameter[3]);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
static Ordering ordering(Generic generic) {
|
||||
Variable v=generic.variableValue();
|
||||
if(v.compareTo(new Constant("lex"))==0) return Monomial.lexicographic;
|
||||
else if(v.compareTo(new Constant("tdl"))==0) return Monomial.totalDegreeLexicographic;
|
||||
else if(v.compareTo(new Constant("drl"))==0) return Monomial.degreeReverseLexicographic;
|
||||
else if(v instanceof ImplicitFunction) {
|
||||
Generic g[]=((ImplicitFunction)v).parameters();
|
||||
int k=g[0].integerValue().intValue();
|
||||
if(v.compareTo(new ImplicitFunction("elim",new Generic[] {JSCLInteger.valueOf(k)},new int[] {0},new Generic[] {}))==0) return Monomial.kthElimination(k);
|
||||
}
|
||||
throw new ArithmeticException();
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer buffer=new StringBuffer();
|
||||
int n=4;
|
||||
if(parameter[3].signum()==0) {
|
||||
n=3;
|
||||
if(ordering(parameter[2])==Monomial.lexicographic) n=2;
|
||||
}
|
||||
buffer.append(name);
|
||||
buffer.append("(");
|
||||
for(int i=0;i<n;i++) {
|
||||
buffer.append(parameter[i]).append(i<n-1?", ":"");
|
||||
}
|
||||
buffer.append(")");
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
public void toMathML(MathML element, Object data) {
|
||||
MathML e1;
|
||||
int exponent=data instanceof Integer?((Integer)data).intValue():1;
|
||||
int n=4;
|
||||
if(parameter[3].signum()==0) {
|
||||
n=3;
|
||||
if(ordering(parameter[2])==Monomial.lexicographic) n=2;
|
||||
}
|
||||
if(exponent==1) nameToMathML(element);
|
||||
else {
|
||||
e1=element.element("msup");
|
||||
nameToMathML(e1);
|
||||
MathML e2=element.element("mn");
|
||||
e2.appendChild(element.text(String.valueOf(exponent)));
|
||||
e1.appendChild(e2);
|
||||
element.appendChild(e1);
|
||||
}
|
||||
e1=element.element("mfenced");
|
||||
for(int i=0;i<n;i++) {
|
||||
parameter[i].toMathML(e1,null);
|
||||
}
|
||||
element.appendChild(e1);
|
||||
}
|
||||
|
||||
protected Variable newinstance() {
|
||||
return new Groebner(null,null,null,null);
|
||||
}
|
||||
}
|
||||
|
||||
class PolynomialVector extends JSCLVector {
|
||||
final Basis basis;
|
||||
|
||||
PolynomialVector(Basis basis) {
|
||||
this(basis,basis.elements());
|
||||
}
|
||||
|
||||
PolynomialVector(Basis basis, Generic generic[]) {
|
||||
super(generic.length>0?generic:new Generic[] {JSCLInteger.valueOf(0)});
|
||||
this.basis=basis;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer buffer=new StringBuffer();
|
||||
buffer.append("{");
|
||||
for(int i=0;i<n;i++) {
|
||||
buffer.append(basis.polynomial(element[i])).append(i<n-1?", ":"");
|
||||
}
|
||||
buffer.append("}");
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
protected void bodyToMathML(MathML e0) {
|
||||
MathML e1=e0.element("mfenced");
|
||||
MathML e2=e0.element("mtable");
|
||||
for(int i=0;i<n;i++) {
|
||||
MathML e3=e0.element("mtr");
|
||||
MathML e4=e0.element("mtd");
|
||||
basis.polynomial(element[i]).toMathML(e4,null);
|
||||
e3.appendChild(e4);
|
||||
e2.appendChild(e3);
|
||||
}
|
||||
e1.appendChild(e2);
|
||||
e0.appendChild(e1);
|
||||
}
|
||||
|
||||
protected Generic newinstance(Generic element[]) {
|
||||
return new PolynomialVector(basis,element);
|
||||
}
|
||||
}
|
53
src/misc/src/jscl/math/operator/IndefiniteIntegral.java
Normal file
53
src/misc/src/jscl/math/operator/IndefiniteIntegral.java
Normal file
@ -0,0 +1,53 @@
|
||||
package jscl.math.operator;
|
||||
|
||||
import jscl.math.Generic;
|
||||
import jscl.math.NotIntegrableException;
|
||||
import jscl.math.Variable;
|
||||
import jscl.mathml.MathML;
|
||||
|
||||
public class IndefiniteIntegral extends Operator {
|
||||
public IndefiniteIntegral(Generic expression, Generic variable) {
|
||||
super("integral",new Generic[] {expression,variable});
|
||||
}
|
||||
|
||||
public Generic compute() {
|
||||
Variable variable=parameter[1].variableValue();
|
||||
try {
|
||||
return parameter[0].antiderivative(variable);
|
||||
} catch (NotIntegrableException e) {}
|
||||
return expressionValue();
|
||||
}
|
||||
|
||||
public void toMathML(MathML element, Object data) {
|
||||
int exponent=data instanceof Integer?((Integer)data).intValue():1;
|
||||
if(exponent==1) bodyToMathML(element);
|
||||
else {
|
||||
MathML e1=element.element("msup");
|
||||
MathML e2=element.element("mfenced");
|
||||
bodyToMathML(e2);
|
||||
e1.appendChild(e2);
|
||||
e2=element.element("mn");
|
||||
e2.appendChild(element.text(String.valueOf(exponent)));
|
||||
e1.appendChild(e2);
|
||||
element.appendChild(e1);
|
||||
}
|
||||
}
|
||||
|
||||
void bodyToMathML(MathML element) {
|
||||
Variable v=parameter[1].variableValue();
|
||||
MathML e1=element.element("mrow");
|
||||
MathML e2=element.element("mo");
|
||||
e2.appendChild(element.text("\u222B"));
|
||||
e1.appendChild(e2);
|
||||
parameter[0].toMathML(e1,null);
|
||||
e2=element.element("mo");
|
||||
e2.appendChild(element.text(/*"\u2146"*/"d"));
|
||||
e1.appendChild(e2);
|
||||
v.toMathML(e1,null);
|
||||
element.appendChild(e1);
|
||||
}
|
||||
|
||||
protected Variable newinstance() {
|
||||
return new IndefiniteIntegral(null,null);
|
||||
}
|
||||
}
|
58
src/misc/src/jscl/math/operator/Integral.java
Normal file
58
src/misc/src/jscl/math/operator/Integral.java
Normal file
@ -0,0 +1,58 @@
|
||||
package jscl.math.operator;
|
||||
|
||||
import jscl.math.Generic;
|
||||
import jscl.math.NotIntegrableException;
|
||||
import jscl.math.Variable;
|
||||
import jscl.mathml.MathML;
|
||||
|
||||
public class Integral extends Operator {
|
||||
public Integral(Generic expression, Generic variable, Generic n1, Generic n2) {
|
||||
super("integral",new Generic[] {expression,variable,n1,n2});
|
||||
}
|
||||
|
||||
public Generic compute() {
|
||||
Variable variable=parameter[1].variableValue();
|
||||
try {
|
||||
Generic a=parameter[0].antiderivative(variable);
|
||||
return a.substitute(variable,parameter[3]).subtract(a.substitute(variable,parameter[2]));
|
||||
} catch (NotIntegrableException e) {}
|
||||
return expressionValue();
|
||||
}
|
||||
|
||||
public void toMathML(MathML element, Object data) {
|
||||
int exponent=data instanceof Integer?((Integer)data).intValue():1;
|
||||
if(exponent==1) bodyToMathML(element);
|
||||
else {
|
||||
MathML e1=element.element("msup");
|
||||
MathML e2=element.element("mfenced");
|
||||
bodyToMathML(e2);
|
||||
e1.appendChild(e2);
|
||||
e2=element.element("mn");
|
||||
e2.appendChild(element.text(String.valueOf(exponent)));
|
||||
e1.appendChild(e2);
|
||||
element.appendChild(e1);
|
||||
}
|
||||
}
|
||||
|
||||
void bodyToMathML(MathML element) {
|
||||
Variable v=parameter[1].variableValue();
|
||||
MathML e1=element.element("mrow");
|
||||
MathML e2=element.element("msubsup");
|
||||
MathML e3=element.element("mo");
|
||||
e3.appendChild(element.text("\u222B"));
|
||||
e2.appendChild(e3);
|
||||
parameter[2].toMathML(e2,null);
|
||||
parameter[3].toMathML(e2,null);
|
||||
e1.appendChild(e2);
|
||||
parameter[0].toMathML(e1,null);
|
||||
e2=element.element("mo");
|
||||
e2.appendChild(element.text(/*"\u2146"*/"d"));
|
||||
e1.appendChild(e2);
|
||||
v.toMathML(e1,null);
|
||||
element.appendChild(e1);
|
||||
}
|
||||
|
||||
protected Variable newinstance() {
|
||||
return new Integral(null,null,null,null);
|
||||
}
|
||||
}
|
75
src/misc/src/jscl/math/operator/Limit.java
Normal file
75
src/misc/src/jscl/math/operator/Limit.java
Normal file
@ -0,0 +1,75 @@
|
||||
package jscl.math.operator;
|
||||
|
||||
import jscl.math.Generic;
|
||||
import jscl.math.Variable;
|
||||
import jscl.mathml.MathML;
|
||||
|
||||
public class Limit extends Operator {
|
||||
public Limit(Generic expression, Generic variable, Generic limit, Generic direction) {
|
||||
super("lim",new Generic[] {expression,variable,limit,direction});
|
||||
}
|
||||
|
||||
public Generic compute() {
|
||||
return expressionValue();
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer buffer=new StringBuffer();
|
||||
int n=4;
|
||||
if(parameter[3].signum()==0) n=3;
|
||||
buffer.append(name);
|
||||
buffer.append("(");
|
||||
for(int i=0;i<n;i++) {
|
||||
buffer.append(parameter[i]).append(i<n-1?", ":"");
|
||||
}
|
||||
buffer.append(")");
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
public void toMathML(MathML element, Object data) {
|
||||
int exponent=data instanceof Integer?((Integer)data).intValue():1;
|
||||
if(exponent==1) bodyToMathML(element);
|
||||
else {
|
||||
MathML e1=element.element("msup");
|
||||
MathML e2=element.element("mfenced");
|
||||
bodyToMathML(e2);
|
||||
e1.appendChild(e2);
|
||||
e2=element.element("mn");
|
||||
e2.appendChild(element.text(String.valueOf(exponent)));
|
||||
e1.appendChild(e2);
|
||||
element.appendChild(e1);
|
||||
}
|
||||
}
|
||||
|
||||
void bodyToMathML(MathML element) {
|
||||
int c=parameter[3].signum();
|
||||
MathML e1=element.element("mrow");
|
||||
MathML e2=element.element("munder");
|
||||
MathML e3=element.element("mo");
|
||||
e3.appendChild(element.text("lim"));
|
||||
e2.appendChild(e3);
|
||||
e3=element.element("mrow");
|
||||
parameter[1].toMathML(e3,null);
|
||||
MathML e4=element.element("mo");
|
||||
e4.appendChild(element.text("\u2192"));
|
||||
e3.appendChild(e4);
|
||||
if(c==0) parameter[2].toMathML(e3,null);
|
||||
else {
|
||||
e4=element.element("msup");
|
||||
parameter[2].toMathML(e4,null);
|
||||
MathML e5=element.element("mo");
|
||||
if(c<0) e5.appendChild(element.text("-"));
|
||||
else if(c>0) e5.appendChild(element.text("+"));
|
||||
e4.appendChild(e5);
|
||||
e3.appendChild(e4);
|
||||
}
|
||||
e2.appendChild(e3);
|
||||
e1.appendChild(e2);
|
||||
parameter[0].toMathML(e1,null);
|
||||
element.appendChild(e1);
|
||||
}
|
||||
|
||||
protected Variable newinstance() {
|
||||
return new Limit(null,null,null,null);
|
||||
}
|
||||
}
|
25
src/misc/src/jscl/math/operator/Modulo.java
Normal file
25
src/misc/src/jscl/math/operator/Modulo.java
Normal file
@ -0,0 +1,25 @@
|
||||
package jscl.math.operator;
|
||||
|
||||
import jscl.math.Generic;
|
||||
import jscl.math.JSCLInteger;
|
||||
import jscl.math.NotIntegerException;
|
||||
import jscl.math.Variable;
|
||||
|
||||
public class Modulo extends Operator {
|
||||
public Modulo(Generic expression1, Generic expression2) {
|
||||
super("mod",new Generic[] {expression1,expression2});
|
||||
}
|
||||
|
||||
public Generic compute() {
|
||||
try {
|
||||
JSCLInteger en=parameter[0].integerValue();
|
||||
JSCLInteger en2=parameter[1].integerValue();
|
||||
return en.mod(en2);
|
||||
} catch (NotIntegerException e) {}
|
||||
return parameter[0].remainder(parameter[1]);
|
||||
}
|
||||
|
||||
protected Variable newinstance() {
|
||||
return new Modulo(null,null);
|
||||
}
|
||||
}
|
140
src/misc/src/jscl/math/operator/Operator.java
Normal file
140
src/misc/src/jscl/math/operator/Operator.java
Normal file
@ -0,0 +1,140 @@
|
||||
package jscl.math.operator;
|
||||
|
||||
import jscl.math.Generic;
|
||||
import jscl.math.JSCLInteger;
|
||||
import jscl.math.JSCLVector;
|
||||
import jscl.math.NotIntegrableException;
|
||||
import jscl.math.NotVariableException;
|
||||
import jscl.math.Variable;
|
||||
import jscl.mathml.MathML;
|
||||
import jscl.util.ArrayComparator;
|
||||
|
||||
public abstract class Operator extends Variable {
|
||||
protected Generic parameter[];
|
||||
|
||||
public Operator(String name, Generic parameter[]) {
|
||||
super(name);
|
||||
this.parameter=parameter;
|
||||
}
|
||||
|
||||
public Generic[] parameters() {
|
||||
return parameter;
|
||||
}
|
||||
|
||||
public abstract Generic compute();
|
||||
|
||||
public Generic antiderivative(Variable variable) throws NotIntegrableException {
|
||||
return null;
|
||||
}
|
||||
|
||||
public Generic derivative(Variable variable) {
|
||||
if(isIdentity(variable)) return JSCLInteger.valueOf(1);
|
||||
else return JSCLInteger.valueOf(0);
|
||||
}
|
||||
|
||||
public Generic substitute(Variable variable, Generic generic) {
|
||||
Operator v=(Operator)newinstance();
|
||||
for(int i=0;i<parameter.length;i++) {
|
||||
v.parameter[i]=parameter[i].substitute(variable,generic);
|
||||
}
|
||||
if(v.isIdentity(variable)) return generic;
|
||||
else return v.compute();
|
||||
}
|
||||
|
||||
public Generic expand() {
|
||||
Operator v=(Operator)newinstance();
|
||||
for(int i=0;i<parameter.length;i++) {
|
||||
v.parameter[i]=parameter[i].expand();
|
||||
}
|
||||
return v.compute();
|
||||
}
|
||||
|
||||
public Generic factorize() {
|
||||
Operator v=(Operator)newinstance();
|
||||
for(int i=0;i<parameter.length;i++) {
|
||||
v.parameter[i]=parameter[i].factorize();
|
||||
}
|
||||
return v.expressionValue();
|
||||
}
|
||||
|
||||
public Generic elementary() {
|
||||
Operator v=(Operator)newinstance();
|
||||
for(int i=0;i<parameter.length;i++) {
|
||||
v.parameter[i]=parameter[i].elementary();
|
||||
}
|
||||
return v.expressionValue();
|
||||
}
|
||||
|
||||
public Generic simplify() {
|
||||
Operator v=(Operator)newinstance();
|
||||
for(int i=0;i<parameter.length;i++) {
|
||||
v.parameter[i]=parameter[i].simplify();
|
||||
}
|
||||
return v.expressionValue();
|
||||
}
|
||||
|
||||
public Generic numeric() {
|
||||
throw new ArithmeticException();
|
||||
}
|
||||
|
||||
public boolean isConstant(Variable variable) {
|
||||
return !isIdentity(variable);
|
||||
}
|
||||
|
||||
public int compareTo(Variable variable) {
|
||||
if(this==variable) return 0;
|
||||
int c=comparator.compare(this,variable);
|
||||
if(c<0) return -1;
|
||||
else if(c>0) return 1;
|
||||
else {
|
||||
Operator v=(Operator)variable;
|
||||
c=name.compareTo(v.name);
|
||||
if(c<0) return -1;
|
||||
else if(c>0) return 1;
|
||||
else return ArrayComparator.comparator.compare(parameter,v.parameter);
|
||||
}
|
||||
}
|
||||
|
||||
protected static Variable[] variables(Generic generic) throws NotVariableException {
|
||||
Generic element[]=((JSCLVector)generic).elements();
|
||||
Variable variable[]=new Variable[element.length];
|
||||
for(int i=0;i<element.length;i++) {
|
||||
variable[i]=element[i].variableValue();
|
||||
}
|
||||
return variable;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer buffer=new StringBuffer();
|
||||
buffer.append(name);
|
||||
buffer.append("(");
|
||||
for(int i=0;i<parameter.length;i++) {
|
||||
buffer.append(parameter[i]).append(i<parameter.length-1?", ":"");
|
||||
}
|
||||
buffer.append(")");
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
public String toJava() {
|
||||
throw new ArithmeticException();
|
||||
}
|
||||
|
||||
public void toMathML(MathML element, Object data) {
|
||||
MathML e1;
|
||||
int exponent=data instanceof Integer?((Integer)data).intValue():1;
|
||||
if(exponent==1) nameToMathML(element);
|
||||
else {
|
||||
e1=element.element("msup");
|
||||
nameToMathML(e1);
|
||||
MathML e2=element.element("mn");
|
||||
e2.appendChild(element.text(String.valueOf(exponent)));
|
||||
e1.appendChild(e2);
|
||||
element.appendChild(e1);
|
||||
}
|
||||
e1=element.element("mfenced");
|
||||
for(int i=0;i<parameter.length;i++) {
|
||||
parameter[i].toMathML(e1,null);
|
||||
}
|
||||
element.appendChild(e1);
|
||||
}
|
||||
}
|
65
src/misc/src/jscl/math/operator/Product.java
Normal file
65
src/misc/src/jscl/math/operator/Product.java
Normal file
@ -0,0 +1,65 @@
|
||||
package jscl.math.operator;
|
||||
|
||||
import jscl.math.Generic;
|
||||
import jscl.math.JSCLInteger;
|
||||
import jscl.math.NotIntegerException;
|
||||
import jscl.math.Variable;
|
||||
import jscl.mathml.MathML;
|
||||
|
||||
public class Product extends Operator {
|
||||
public Product(Generic expression, Generic variable, Generic n1, Generic n2) {
|
||||
super("prod",new Generic[] {expression,variable,n1,n2});
|
||||
}
|
||||
|
||||
public Generic compute() {
|
||||
Variable variable=parameter[1].variableValue();
|
||||
try {
|
||||
int n1=parameter[2].integerValue().intValue();
|
||||
int n2=parameter[3].integerValue().intValue();
|
||||
Generic a=JSCLInteger.valueOf(1);
|
||||
for(int i=n1;i<=n2;i++) {
|
||||
a=a.multiply(parameter[0].substitute(variable,JSCLInteger.valueOf(i)));
|
||||
}
|
||||
return a;
|
||||
} catch (NotIntegerException e) {}
|
||||
return expressionValue();
|
||||
}
|
||||
|
||||
public void toMathML(MathML element, Object data) {
|
||||
int exponent=data instanceof Integer?((Integer)data).intValue():1;
|
||||
if(exponent==1) bodyToMathML(element);
|
||||
else {
|
||||
MathML e1=element.element("msup");
|
||||
MathML e2=element.element("mfenced");
|
||||
bodyToMathML(e2);
|
||||
e1.appendChild(e2);
|
||||
e2=element.element("mn");
|
||||
e2.appendChild(element.text(String.valueOf(exponent)));
|
||||
e1.appendChild(e2);
|
||||
element.appendChild(e1);
|
||||
}
|
||||
}
|
||||
|
||||
void bodyToMathML(MathML element) {
|
||||
MathML e1=element.element("mrow");
|
||||
MathML e2=element.element("munderover");
|
||||
MathML e3=element.element("mo");
|
||||
e3.appendChild(element.text("\u220F"));
|
||||
e2.appendChild(e3);
|
||||
e3=element.element("mrow");
|
||||
parameter[1].toMathML(e3,null);
|
||||
MathML e4=element.element("mo");
|
||||
e4.appendChild(element.text("="));
|
||||
e3.appendChild(e4);
|
||||
parameter[2].toMathML(e3,null);
|
||||
e2.appendChild(e3);
|
||||
parameter[3].toMathML(e2,null);
|
||||
e1.appendChild(e2);
|
||||
parameter[0].toMathML(e1,null);
|
||||
element.appendChild(e1);
|
||||
}
|
||||
|
||||
protected Variable newinstance() {
|
||||
return new Product(null,null,null,null);
|
||||
}
|
||||
}
|
61
src/misc/src/jscl/math/operator/Solve.java
Normal file
61
src/misc/src/jscl/math/operator/Solve.java
Normal file
@ -0,0 +1,61 @@
|
||||
package jscl.math.operator;
|
||||
|
||||
import jscl.math.Generic;
|
||||
import jscl.math.Variable;
|
||||
import jscl.math.function.Root;
|
||||
import jscl.math.polynomial.Polynomial;
|
||||
import jscl.math.polynomial.UnivariatePolynomial;
|
||||
import jscl.mathml.MathML;
|
||||
|
||||
public class Solve extends Operator {
|
||||
public Solve(Generic expression, Generic variable, Generic subscript) {
|
||||
super("solve",new Generic[] {expression,variable,subscript});
|
||||
}
|
||||
|
||||
public Generic compute() {
|
||||
Variable variable=parameter[1].variableValue();
|
||||
int subscript=parameter[2].integerValue().intValue();
|
||||
if(parameter[0].isPolynomial(variable)) {
|
||||
return new Root((UnivariatePolynomial)Polynomial.factory(variable).valueof(parameter[0]),subscript).evaluate();
|
||||
}
|
||||
return expressionValue();
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer buffer=new StringBuffer();
|
||||
int n=3;
|
||||
if(parameter[2].signum()==0) n=2;
|
||||
buffer.append(name);
|
||||
buffer.append("(");
|
||||
for(int i=0;i<n;i++) {
|
||||
buffer.append(parameter[i]).append(i<n-1?", ":"");
|
||||
}
|
||||
buffer.append(")");
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
public void toMathML(MathML element, Object data) {
|
||||
MathML e1;
|
||||
int exponent=data instanceof Integer?((Integer)data).intValue():1;
|
||||
int n=3;
|
||||
if(parameter[2].signum()==0) n=2;
|
||||
if(exponent==1) nameToMathML(element);
|
||||
else {
|
||||
e1=element.element("msup");
|
||||
nameToMathML(e1);
|
||||
MathML e2=element.element("mn");
|
||||
e2.appendChild(element.text(String.valueOf(exponent)));
|
||||
e1.appendChild(e2);
|
||||
element.appendChild(e1);
|
||||
}
|
||||
e1=element.element("mfenced");
|
||||
for(int i=0;i<n;i++) {
|
||||
parameter[i].toMathML(e1,null);
|
||||
}
|
||||
element.appendChild(e1);
|
||||
}
|
||||
|
||||
protected Variable newinstance() {
|
||||
return new Solve(null,null,null);
|
||||
}
|
||||
}
|
41
src/misc/src/jscl/math/operator/Substitute.java
Normal file
41
src/misc/src/jscl/math/operator/Substitute.java
Normal file
@ -0,0 +1,41 @@
|
||||
package jscl.math.operator;
|
||||
|
||||
import jscl.math.Generic;
|
||||
import jscl.math.GenericVariable;
|
||||
import jscl.math.JSCLVector;
|
||||
import jscl.math.Variable;
|
||||
|
||||
public class Substitute extends Operator {
|
||||
public Substitute(Generic expression, Generic variable, Generic value) {
|
||||
super("subst",new Generic[] {expression,variable,value});
|
||||
}
|
||||
|
||||
public Generic compute() {
|
||||
if(parameter[1] instanceof JSCLVector && parameter[2] instanceof JSCLVector) {
|
||||
Generic a=parameter[0];
|
||||
Variable variable[]=variables(parameter[1]);
|
||||
Generic s[]=((JSCLVector)parameter[2]).elements();
|
||||
for(int i=0;i<variable.length;i++) a=a.substitute(variable[i],s[i]);
|
||||
return a;
|
||||
} else {
|
||||
Variable variable=parameter[1].variableValue();
|
||||
return parameter[0].substitute(variable,parameter[2]);
|
||||
}
|
||||
}
|
||||
|
||||
public Operator transmute() {
|
||||
Generic p[]=new Generic[] {null,GenericVariable.content(parameter[1]),GenericVariable.content(parameter[2])};
|
||||
if(p[1] instanceof JSCLVector && p[2] instanceof JSCLVector) {
|
||||
return new Substitute(parameter[0],p[1],p[2]);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public Generic expand() {
|
||||
return compute();
|
||||
}
|
||||
|
||||
protected Variable newinstance() {
|
||||
return new Substitute(null,null,null);
|
||||
}
|
||||
}
|
65
src/misc/src/jscl/math/operator/Sum.java
Normal file
65
src/misc/src/jscl/math/operator/Sum.java
Normal file
@ -0,0 +1,65 @@
|
||||
package jscl.math.operator;
|
||||
|
||||
import jscl.math.Generic;
|
||||
import jscl.math.JSCLInteger;
|
||||
import jscl.math.NotIntegerException;
|
||||
import jscl.math.Variable;
|
||||
import jscl.mathml.MathML;
|
||||
|
||||
public class Sum extends Operator {
|
||||
public Sum(Generic expression, Generic variable, Generic n1, Generic n2) {
|
||||
super("sum",new Generic[] {expression,variable,n1,n2});
|
||||
}
|
||||
|
||||
public Generic compute() {
|
||||
Variable variable=parameter[1].variableValue();
|
||||
try {
|
||||
int n1=parameter[2].integerValue().intValue();
|
||||
int n2=parameter[3].integerValue().intValue();
|
||||
Generic a=JSCLInteger.valueOf(0);
|
||||
for(int i=n1;i<=n2;i++) {
|
||||
a=a.add(parameter[0].substitute(variable,JSCLInteger.valueOf(i)));
|
||||
}
|
||||
return a;
|
||||
} catch (NotIntegerException e) {}
|
||||
return expressionValue();
|
||||
}
|
||||
|
||||
public void toMathML(MathML element, Object data) {
|
||||
int exponent=data instanceof Integer?((Integer)data).intValue():1;
|
||||
if(exponent==1) bodyToMathML(element);
|
||||
else {
|
||||
MathML e1=element.element("msup");
|
||||
MathML e2=element.element("mfenced");
|
||||
bodyToMathML(e2);
|
||||
e1.appendChild(e2);
|
||||
e2=element.element("mn");
|
||||
e2.appendChild(element.text(String.valueOf(exponent)));
|
||||
e1.appendChild(e2);
|
||||
element.appendChild(e1);
|
||||
}
|
||||
}
|
||||
|
||||
void bodyToMathML(MathML element) {
|
||||
MathML e1=element.element("mrow");
|
||||
MathML e2=element.element("munderover");
|
||||
MathML e3=element.element("mo");
|
||||
e3.appendChild(element.text("\u2211"));
|
||||
e2.appendChild(e3);
|
||||
e3=element.element("mrow");
|
||||
parameter[1].toMathML(e3,null);
|
||||
MathML e4=element.element("mo");
|
||||
e4.appendChild(element.text("="));
|
||||
e3.appendChild(e4);
|
||||
parameter[2].toMathML(e3,null);
|
||||
e2.appendChild(e3);
|
||||
parameter[3].toMathML(e2,null);
|
||||
e1.appendChild(e2);
|
||||
parameter[0].toMathML(e1,null);
|
||||
element.appendChild(e1);
|
||||
}
|
||||
|
||||
protected Variable newinstance() {
|
||||
return new Sum(null,null,null,null);
|
||||
}
|
||||
}
|
40
src/misc/src/jscl/math/operator/VectorOperator.java
Normal file
40
src/misc/src/jscl/math/operator/VectorOperator.java
Normal file
@ -0,0 +1,40 @@
|
||||
package jscl.math.operator;
|
||||
|
||||
import jscl.math.Generic;
|
||||
import jscl.math.GenericVariable;
|
||||
import jscl.math.Variable;
|
||||
import jscl.math.function.Constant;
|
||||
import jscl.mathml.MathML;
|
||||
|
||||
public abstract class VectorOperator extends Operator {
|
||||
public VectorOperator(String name, Generic parameter[]) {
|
||||
super(name,parameter);
|
||||
}
|
||||
|
||||
public void toMathML(MathML element, Object data) {
|
||||
int exponent=data instanceof Integer?((Integer)data).intValue():1;
|
||||
if(exponent==1) bodyToMathML(element);
|
||||
else {
|
||||
MathML e1=element.element("msup");
|
||||
MathML e2=element.element("mfenced");
|
||||
bodyToMathML(e2);
|
||||
e1.appendChild(e2);
|
||||
e2=element.element("mn");
|
||||
e2.appendChild(element.text(String.valueOf(exponent)));
|
||||
e1.appendChild(e2);
|
||||
element.appendChild(e1);
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract void bodyToMathML(MathML element);
|
||||
|
||||
protected void operator(MathML element, String name) {
|
||||
Variable variable[]=variables(GenericVariable.content(parameter[1]));
|
||||
MathML e1=element.element("msub");
|
||||
new Constant(name).toMathML(e1,null);
|
||||
MathML e2=element.element("mrow");
|
||||
for(int i=0;i<variable.length;i++) variable[i].expressionValue().toMathML(e2,null);
|
||||
e1.appendChild(e2);
|
||||
element.appendChild(e1);
|
||||
}
|
||||
}
|
61
src/misc/src/jscl/math/operator/matrix/Determinant.java
Normal file
61
src/misc/src/jscl/math/operator/matrix/Determinant.java
Normal file
@ -0,0 +1,61 @@
|
||||
package jscl.math.operator.matrix;
|
||||
|
||||
import jscl.math.Generic;
|
||||
import jscl.math.GenericVariable;
|
||||
import jscl.math.Matrix;
|
||||
import jscl.math.Variable;
|
||||
import jscl.math.operator.Operator;
|
||||
import jscl.mathml.MathML;
|
||||
|
||||
public class Determinant extends Operator {
|
||||
public Determinant(Generic matrix) {
|
||||
super("det",new Generic[] {matrix});
|
||||
}
|
||||
|
||||
public Generic compute() {
|
||||
if(parameter[0] instanceof Matrix) {
|
||||
Matrix matrix=(Matrix)parameter[0];
|
||||
return matrix.determinant();
|
||||
}
|
||||
return expressionValue();
|
||||
}
|
||||
|
||||
public void toMathML(MathML element, Object data) {
|
||||
int exponent=data instanceof Integer?((Integer)data).intValue():1;
|
||||
if(exponent==1) bodyToMathML(element);
|
||||
else {
|
||||
MathML e1=element.element("msup");
|
||||
bodyToMathML(e1);
|
||||
MathML e2=element.element("mn");
|
||||
e2.appendChild(element.text(String.valueOf(exponent)));
|
||||
e1.appendChild(e2);
|
||||
element.appendChild(e1);
|
||||
}
|
||||
}
|
||||
|
||||
void bodyToMathML(MathML e0) {
|
||||
Generic m=GenericVariable.content(parameter[0]);
|
||||
MathML e1=e0.element("mfenced");
|
||||
e1.setAttribute("open","|");
|
||||
e1.setAttribute("close","|");
|
||||
if(m instanceof Matrix) {
|
||||
Generic element[][]=((Matrix)m).elements();
|
||||
MathML e2=e0.element("mtable");
|
||||
for(int i=0;i<element.length;i++) {
|
||||
MathML e3=e0.element("mtr");
|
||||
for(int j=0;j<element.length;j++) {
|
||||
MathML e4=e0.element("mtd");
|
||||
element[i][j].toMathML(e4,null);
|
||||
e3.appendChild(e4);
|
||||
}
|
||||
e2.appendChild(e3);
|
||||
}
|
||||
e1.appendChild(e2);
|
||||
} else m.toMathML(e1,null);
|
||||
e0.appendChild(e1);
|
||||
}
|
||||
|
||||
protected Variable newinstance() {
|
||||
return new Determinant(null);
|
||||
}
|
||||
}
|
45
src/misc/src/jscl/math/operator/matrix/Trace.java
Normal file
45
src/misc/src/jscl/math/operator/matrix/Trace.java
Normal file
@ -0,0 +1,45 @@
|
||||
package jscl.math.operator.matrix;
|
||||
|
||||
import jscl.math.Generic;
|
||||
import jscl.math.Matrix;
|
||||
import jscl.math.Variable;
|
||||
import jscl.math.operator.Operator;
|
||||
import jscl.mathml.MathML;
|
||||
|
||||
public class Trace extends Operator {
|
||||
public Trace(Generic matrix) {
|
||||
super("trace",new Generic[] {matrix});
|
||||
}
|
||||
|
||||
public Generic compute() {
|
||||
if(parameter[0] instanceof Matrix) {
|
||||
Matrix matrix=(Matrix)parameter[0];
|
||||
return matrix.trace();
|
||||
}
|
||||
return expressionValue();
|
||||
}
|
||||
|
||||
public void toMathML(MathML element, Object data) {
|
||||
int exponent=data instanceof Integer?((Integer)data).intValue():1;
|
||||
if(exponent==1) {
|
||||
MathML e1=element.element("mo");
|
||||
e1.appendChild(element.text("tr"));
|
||||
element.appendChild(e1);
|
||||
}
|
||||
else {
|
||||
MathML e1=element.element("msup");
|
||||
MathML e2=element.element("mo");
|
||||
e2.appendChild(element.text("tr"));
|
||||
e1.appendChild(e2);
|
||||
e2=element.element("mn");
|
||||
e2.appendChild(element.text(String.valueOf(exponent)));
|
||||
e1.appendChild(e2);
|
||||
element.appendChild(e1);
|
||||
}
|
||||
parameter[0].toMathML(element,null);
|
||||
}
|
||||
|
||||
protected Variable newinstance() {
|
||||
return new Trace(null);
|
||||
}
|
||||
}
|
47
src/misc/src/jscl/math/operator/matrix/Transpose.java
Normal file
47
src/misc/src/jscl/math/operator/matrix/Transpose.java
Normal file
@ -0,0 +1,47 @@
|
||||
package jscl.math.operator.matrix;
|
||||
|
||||
import jscl.math.Generic;
|
||||
import jscl.math.Matrix;
|
||||
import jscl.math.Variable;
|
||||
import jscl.math.operator.Operator;
|
||||
import jscl.mathml.MathML;
|
||||
|
||||
public class Transpose extends Operator {
|
||||
public Transpose(Generic matrix) {
|
||||
super("tran",new Generic[] {matrix});
|
||||
}
|
||||
|
||||
public Generic compute() {
|
||||
if(parameter[0] instanceof Matrix) {
|
||||
Matrix matrix=(Matrix)parameter[0];
|
||||
return matrix.transpose();
|
||||
}
|
||||
return expressionValue();
|
||||
}
|
||||
|
||||
public void toMathML(MathML element, Object data) {
|
||||
int exponent=data instanceof Integer?((Integer)data).intValue():1;
|
||||
if(exponent==1) bodyToMathML(element);
|
||||
else {
|
||||
MathML e1=element.element("msup");
|
||||
bodyToMathML(e1);
|
||||
MathML e2=element.element("mn");
|
||||
e2.appendChild(element.text(String.valueOf(exponent)));
|
||||
e1.appendChild(e2);
|
||||
element.appendChild(e1);
|
||||
}
|
||||
}
|
||||
|
||||
void bodyToMathML(MathML element) {
|
||||
MathML e1=element.element("msup");
|
||||
parameter[0].toMathML(e1,null);
|
||||
MathML e2=element.element("mo");
|
||||
e2.appendChild(element.text("T"));
|
||||
e1.appendChild(e2);
|
||||
element.appendChild(e1);
|
||||
}
|
||||
|
||||
protected Variable newinstance() {
|
||||
return new Transpose(null);
|
||||
}
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user