All Downloads are FREE. Search and download functionalities are using the official Maven repository.

JSci.maths.symbolic.Variable Maven / Gradle / Ivy

Go to download

JSci is a set of open source Java packages. The aim is to encapsulate scientific methods/principles in the most natural way possible. As such they should greatly aid the development of scientific based software. It offers: abstract math interfaces, linear algebra (support for various matrix and vector types), statistics (including probability distributions), wavelets, newtonian mechanics, chart/graph components (AWT and Swing), MathML DOM implementation, ... Note: some packages, like javax.comm, for the astro and instruments package aren't listed as dependencies (not available).

The newest version!
package JSci.maths.symbolic;
 
import JSci.maths.*;
import JSci.maths.groups.*;
import JSci.maths.fields.*;

import java.util.*;

/** Variables in an Expression. */
public class Variable extends Expression {
    private final String name;
    private final Object valueSet;
    private Member value=null;

    /** 
     * @param n the name (symbol) of the variable
     * @param valueSet the set to which the variable values belong,
     * e.g. RealField.getInstance(). Note 
     * it is not the Class of the values (odd thing indeed).
     */
    public Variable(String n,Object valueSet) {
	name=n;
	this.valueSet=valueSet;
    }

    /** Set the value of the variable. 
     * @param o the value; can be null to unset the variable
     */
    public void setValue(Member o) {
	if (o==null) { value=null; return; }
	if (valueSet.getClass().isInstance(o.getSet()))
	    value=o;
	else
	    throw new ClassCastException("Variable "+this+" set to "+o.getSet()+" value ("+valueSet+" required.");
    }

    /** Get the value of the variable. 
     * @return the value of the variable; null if the variable is unset.
     */
    public Member getValue() {
	return value;
    }

    public boolean equals(Object o) {
	if (!(o instanceof Variable)) return false;
	else return this==o;
    }

    public String toString() { return name; }

    public Expression differentiate(Variable x) {
	if (this.equals(x)) return new Constant(((Ring)valueSet).one());
	else return new Constant(((AbelianGroup)valueSet).zero());
    }

    public Expression evaluate() {
	if (value==null) return this;
	if (value instanceof Expression) return ((Expression)value).evaluate();
	return new Constant(value);
    }

    protected int getPriority() {return 20;}

    public Object getSet() { return (AbelianGroup)valueSet; }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy