1 package de.congrace.exp4j;
4 * Represents a generic variable which can have double or array values.
5 * Optionally the start and step values corresponding to each array index can be specified for array values
6 * Tries to do something sensible if you try and apply a regular function / operator to an array
9 public class Variable {
11 // The primary or preferred representation
12 public enum Primary {DOUBLE, ARRAY, PLACEHOLDER};
13 private final Primary primary;
15 private final String name;
17 private final double doubleValue;
18 private final double[] arrayValue;
20 private final double start, step;
23 * Initialize a new variable with a name only. This can be used as a place holder
25 public Variable(String name){
27 this.primary = Primary.PLACEHOLDER;
28 this.doubleValue = Double.NaN;
29 this.arrayValue = new double[] {Double.NaN};
30 this.start = Double.NaN;
31 this.step = Double.NaN;
35 * Initialize a new double variable
37 public Variable(String name, double d){
39 this.arrayValue = new double[] {d};
41 this.primary = Primary.DOUBLE;
42 this.start = Double.NaN;
43 this.step = Double.NaN;
47 * Initialize a new array variable
49 public Variable(String name, double[] d){
51 this.doubleValue = d[0];
53 this.primary = Primary.ARRAY;
54 this.start = Double.NaN;
55 this.step = Double.NaN;
59 * Initialize a new array variable, specifying the start and step values
61 public Variable(String name, double[] d, double start, double step){
63 this.doubleValue = d[0];
65 this.primary = Primary.ARRAY;
70 public String getName(){
74 public Primary getPrimary(){
78 public double getDoubleValue(){
82 public double[] getArrayValue(){
86 public double getStep(){
90 public double getStart(){
94 public String toString(){
95 if ( arrayValue.length > 1 ){
96 String out = name + " is Array (length " + new Integer(arrayValue.length).toString() + ") : {";
97 for (double x : arrayValue){
100 out = out.substring(0, out.length()-1);
104 return name + " is double : {" + new Double(doubleValue).toString() + "}";