/** * 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); }
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); }
/** * 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; }