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; }
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 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]); }
/* * 有泛型的情况 * 使得程序具有更好的可读性和安全性 */ private static void test1() throws Exception { // 代码更具有可读性. Generic<String> bc = new Generic(); String s = "OK"; File file = new File("e:/1.txt"); // 问题:在添加元素时,会检查错误,只可以向数组列表中添加指定类的对象. bc.add(s); // 由于加入的元素不是String类型,故报错. // bc.add(file);//error The method add(String) in the type Generic<String> is not applicable // for the arguments (File) /* * 不用强制转换 * 擦除get的返回类型后将返回Object类型.编译器自动插入String的强制类型转换. * 也就说,编译器把这个方法调用翻译为两条虚拟机指令: * 1.对原始方法get的调用. * 2.将返回的Object类型强制转换为String类型. */ String s1 = bc.get(0); }
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; }