/** * trigonometric functions - calculate the secans of this token * * @param double value * @return the result as an OperandToken */ public double[] evaluateValue(double[] arg) { DoubleNumberToken num = new DoubleNumberToken(); cos cosF = new cos(); double[] temp = cosF.evaluateValue(arg); double[] result = num.divide(new double[] {1, 0}, temp); return result; }
/** * standard function for evaluation of general external functions * * @param operands * @param pointer to the global values (interpreter, function manager, graphics,...) * @return */ public OperandToken evaluate(Token[] operands, GlobalValues globals) { // function works for one argument only if (getNArgIn(operands) != 1) throwMathLibException(name + " number of arguments < 1"); // works on numbers only if (!(operands[0] instanceof DoubleNumberToken)) throwMathLibException(name + " only works on numbers"); // get number token DoubleNumberToken numOp = (DoubleNumberToken) operands[0]; // get dimension of number token (2dimensional, 3dim, ....) int[] dim = numOp.getSize(); // ceate array of correct size with dimensions "dim" DoubleNumberToken num = new DoubleNumberToken(dim, null, null); // call element evaluation for all values inside the DoubleNumberToken for (int i = 0; i < numOp.getNumberOfElements(); i++) { num.setValueComplex(i, evaluateValue(numOp.getValueComplex(i))); } return num; } // end evaluate
/** * rounds the element to the nearest element towards zero. * * @param operands[0] = value to round * @return a matrix of the same size as the operands */ public OperandToken evaluate(Token[] operands) { // exactly one operand if (getNArgIn(operands) != 1) throwMathLibException("fix: number of arguments != 1"); // only works on numbers if (!(operands[0] instanceof DoubleNumberToken)) throwMathLibException("fix: only works on numbers"); DoubleNumberToken matrix = ((DoubleNumberToken) operands[0]); OperandToken temp = ((OperandToken) matrix.clone()); double[][] reValues = matrix.getValuesRe(); double[][] imValues = matrix.getValuesIm(); for (int y = 0; y < matrix.getSizeY(); y++) { for (int x = 0; x < matrix.getSizeX(); x++) { if (reValues[y][x] >= 0) { // e.g. fix(3.2) => 3 reValues[y][x] = java.lang.Math.floor(reValues[y][x]); } else { // e.g. fix(-3.2) => -3 reValues[y][x] = java.lang.Math.ceil(reValues[y][x]); } if (imValues[y][x] >= 0) { // e.g. fix(3.2i) => 3 imValues[y][x] = java.lang.Math.floor(imValues[y][x]); } else { // e.g. fix(-3.2i) => -3 imValues[y][x] = java.lang.Math.ceil(imValues[y][x]); } } } return new DoubleNumberToken(reValues, imValues); }
/** * return a matrix * * @param operands[0] = number of rows * @param operands[1] = number of columns */ public OperandToken evaluate(Token[] operands) { // at least one operand if (getNArgIn(operands) < 1) throwMathLibException("ones: number of arguments <1 "); // number of arguments int n = getNArgIn(operands); // set up dimension array int[] dim = new int[n]; // only DoubleNumberTokens accepted // each token is one dimension for (int i = 0; i < n; i++) { if (!(operands[i] instanceof DoubleNumberToken)) throwMathLibException("ones: arguments must be numbers"); // get requested dimension dim[i] = (int) ((DoubleNumberToken) operands[i]).getValueRe(); if (dim[i] < 0) throwMathLibException("ones: dimension <0"); } // special case for rand(k) -> rand(k,k) if (dim.length == 1) { int d = dim[0]; dim = new int[] {d, d}; } // ceate array of correct size with dimensions "dim" DoubleNumberToken num = new DoubleNumberToken(dim, null, null); // create "1" value for all values of num for (int i = 0; i < num.getNumberOfElements(); i++) { num.setValue(i, 1, 0); } return num; } // end eval
/** * calculate the number of permutations * * @param operand[0] = the order of the equation * @return the coefficients as a vector */ public OperandToken evaluate(Token[] operands) { OperandToken result = new DoubleNumberToken(0); if (operands.length >= 1 && operands[0] instanceof DoubleNumberToken) { double val = ((DoubleNumberToken) operands[0]).getValueRe(); int order = (new Double(val)).intValue(); double[][] results = new double[1][order + 1]; DoubleNumberToken total = ((DoubleNumberToken) operands[0]); for (int count = 0; count <= order; count++) { // comb(x y) = y!/(x! * (y-x)!) DoubleNumberToken objects = new DoubleNumberToken(count); // result = x! OperandToken temp = objects.factorial(); // temp2 = y-x OperandToken temp2 = ((OperandToken) total.clone()); temp2 = temp2.subtract(objects); // temp2 = (y-x)! temp2 = temp2.factorial(); // temp = x! * (y-x)! temp = temp.multiply(temp2); // temp2 = y! / (x! * (y-x)!) temp2 = total.factorial(); temp2 = temp2.divide(temp); results[0][count] = ((DoubleNumberToken) temp2).getValueRe(); } result = new DoubleNumberToken(results); } return result; }
public OperandToken evaluate(Token[] operands) { // exactly one operand if (getNArgIn(operands) != 1) throwMathLibException("fix: number of arguments != 1"); // only works on numbers if (!(operands[0] instanceof DoubleNumberToken)) throwMathLibException("fix: only works on numbers"); Frame f = new Frame(); ModalDialog m = new ModalDialog(f, "kk", "What do you want?", new String[] {"ok", "not ok", "maybe"}); // f.show(); m.show(); debugLine("questdlg: " + m.str); DoubleNumberToken matrix = ((DoubleNumberToken) operands[0]); OperandToken temp = ((OperandToken) matrix.clone()); double[][] reValues = matrix.getValuesRe(); double[][] imValues = matrix.getValuesIm(); for (int y = 0; y < matrix.getSizeY(); y++) { for (int x = 0; x < matrix.getSizeX(); x++) { if (reValues[y][x] >= 0) { // e.g. fix(3.2) => 3 reValues[y][x] = java.lang.Math.floor(reValues[y][x]); } else { // e.g. fix(-3.2) => -3 reValues[y][x] = java.lang.Math.ceil(reValues[y][x]); } if (imValues[y][x] >= 0) { // e.g. fix(3.2i) => 3 imValues[y][x] = java.lang.Math.floor(imValues[y][x]); } else { // e.g. fix(-3.2i) => -3 imValues[y][x] = java.lang.Math.ceil(imValues[y][x]); } } } return new DoubleNumberToken(reValues, imValues); }