00001 package com.graphbuilder.math; 00002 00003 import com.graphbuilder.struc.Bag; 00004 00008 public class FuncNode extends TermNode { 00009 00010 private Bag bag = new Bag(1); 00011 private double[] of = new double[1]; 00012 00013 public FuncNode(String name, boolean negate) { 00014 super(name, negate); 00015 } 00016 00020 public void add(Expression x) { 00021 insert(x, bag.size()); 00022 } 00023 00027 public void insert(Expression x, int i) { 00028 checkBeforeAccept(x); 00029 int oldCap = bag.getCapacity(); 00030 bag.insert(x, i); 00031 int newCap = bag.getCapacity(); 00032 00033 if (oldCap != newCap) 00034 of = new double[newCap]; 00035 00036 x.parent = this; 00037 } 00038 00042 public void remove(Expression x) { 00043 int size = bag.size(); 00044 bag.remove(x); 00045 if (size != bag.size()) 00046 x.parent = null; 00047 } 00048 00052 public int numChildren() { 00053 return bag.size(); 00054 } 00055 00059 public Expression child(int i) { 00060 return (Expression) bag.get(i); 00061 } 00062 00069 public double eval(VarMap v, FuncMap f) { 00070 int numParam = bag.size(); 00071 00072 for (int i = 0; i < numParam; i++) 00073 of[i] = child(i).eval(v, f); 00074 00075 double result = f.getFunction(name, numParam).of(of, numParam); 00076 00077 if (negate) result = -result; 00078 00079 return result; 00080 } 00081 }