Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/hartmath/initial/SymbolOperators.java


1   /*
2    *  SymbolOperators.java
3    *  Copyright (C) 2000 Klaus Hartlage
4    *
5    *  This program is free software; you can redistribute it and/or
6    *  modify it under the terms of the GNU General Public License
7    *  as published by the Free Software Foundation; either version 2
8    *  of the License, or any later version.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18   */
19  package com.hartmath.initial;
20  import com.hartmath.util.*;
21  import com.hartmath.mapping.*;
22  import com.hartmath.patternmatching.*;
23  
24  import java.net.URL;
25  import java.lang.*;
26  import java.util.Enumeration;
27  import java.io.*;
28  import java.util.*;
29  import com.hartmath.lib.C;
30  import com.hartmath.expression.HObject;
31  import com.hartmath.expression.HSymbol;
32  import com.hartmath.expression.HFunction;
33  import com.hartmath.lib.Session;
34  /*
35   *  class EComplement implements FunctionEvaluator
36   *  {
37   *  public HObject evaluate(HFunction vect)
38   *  {
39   *  if (vect.size() >= 2)
40   *  {
41   *  if (!(vect.get(0) instanceof HFunction))
42   *  {
43   *  return null;
44   *  }
45   *  HashSet        set0 = new HashSet();
46   *  HFunction      f = (HFunction) vect.get(0);
47   *  HFunction      result = C.List.f();
48   *  for (int i = 0; i < f.size(); i++)
49   *  {
50   *  set0.add(f.get(i));
51   *  }
52   *  HFunction      lst;
53   *  HashSet        set1;
54   *  for (int i = 1; i < vect.size(); i++)
55   *  {
56   *  if (vect.get(i) instanceof HFunction)
57   *  {
58   *  lst = (HFunction) vect.get(i);
59   *  set1 = new HashSet();
60   *  for (int j = 0; j < lst.size(); j++)
61   *  {
62   *  set1.add(lst.get(j));
63   *  }
64   *  set0 = set0.difference(set1);
65   *  }
66   *  else
67   *  {
68   *  return null;
69   *  }
70   *  }
71   *  Enumeration    e = set0.elements();
72   *  while (e.hasMoreElements())
73   *  {
74   *  result.add((HObject)e.nextElement());
75   *  }
76   *  result.sort();
77   *  return result;
78   *  }
79   *  return null;
80   *  }
81   *  }
82   */
83  /*
84   *  class EIntersection implements FunctionEvaluator
85   *  {
86   *  public HObject evaluate(HFunction vect)
87   *  {
88   *  if (vect.size() >= 2)
89   *  {
90   *  if (!(vect.get(0) instanceof HFunction))
91   *  {
92   *  return null;
93   *  }
94   *  HashSet        set0 = new HashSet();
95   *  HFunction      f = (HFunction) vect.get(0);
96   *  HFunction      result = C.List.f();
97   *  for (int i = 0; i < f.size(); i++)
98   *  {
99   *  set0.add(f.get(i));
100  *  }
101  *  HFunction      lst;
102  *  HashSet        set1;
103  *  for (int i = 1; i < vect.size(); i++)
104  *  {
105  *  if (vect.get(i) instanceof HFunction)
106  *  {
107  *  lst = (HFunction) vect.get(i);
108  *  set1 = new HashSet();
109  *  for (int j = 0; j < lst.size(); j++)
110  *  {
111  *  set1.add(lst.get(j));
112  *  }
113  *  set0 = set0.intersection(set1);
114  *  }
115  *  else
116  *  {
117  *  return null;
118  *  }
119  *  }
120  *  Enumeration    e = set0.elements();
121  *  while (e.hasMoreElements())
122  *  {
123  *  result.add((HObject)e.nextElement());
124  *  }
125  *  result.sort();
126  *  return result;
127  *  }
128  *  return null;
129  *  }
130  *  }
131  */
132 /**
133  *  Class declaration
134  *
135  *@author
136  *@created    16. Juli 2001
137  *@version    %I%, %G%
138  */
139 class EAddTo extends SFunctionEvaluator implements FunctionOpEvaluator {
140         /**
141          *  Description of the Method
142          *
143          *@param  f        Description of Parameter
144          *@param  session  Description of Parameter
145          *@return          Description of the Returned Value
146          */
147         public HObject evaluate(HFunction f, Session session) {
148                 if (f.size() == 2) {
149                         if (f.get(0) instanceof HSymbol) {
150                                 HObject v1 = session.EV(f.get(1));
151                                 HSymbol sym = (HSymbol) f.get(0);
152                                 HObject result;
153                                 Object temp;
154 
155                                 if (!sym.hasNoLocalVar(session)) {
156                                         result = sym.deleteLocalVar(session);
157                                         temp = execute(result, v1, session);
158 
159                                         if (temp != null) {
160                                                 sym.createLocalVar(session, (HObject) temp);
161 
162                                                 return (HObject) temp;
163                                         }
164 
165                                         sym.createLocalVar(session, result);
166 
167                                         return null;
168                                 }
169 
170                                 result = sym.getDownRule(sym, session);
171 
172                                 if (result != null) {
173                                         temp = execute(result, v1, session);
174 
175                                         if (temp != null) {
176                                                 sym.putDownRule(session, HRule.SET, sym, (HObject) temp);
177 
178                                                 return (HObject) temp;
179                                         }
180                                 }
181                         }
182                 }
183 
184                 return null;
185         }
186 
187 
188         /**
189          *  Method declaration
190          *
191          *@param  first
192          *@param  second
193          *@param  session  Description of Parameter
194          *@return
195          *@see
196          */
197         public Object execute(Object first, Object second, Session session) {
198                 return session.EV(C.Add.f((HObject) first, (HObject) second));
199         }
200 
201 
202         /**
203          *  Method declaration
204          *
205          *@param  fun
206          *@param  session  Description of Parameter
207          *@return
208          *@see
209          */
210         public String toOpString(HFunction fun, Session session) {
211                 return E2SArg.operatorString(fun, "+=", C.ADDTO_PRECEDENCE, session);
212         }
213 
214 
215         /**
216          *  Method declaration
217          *
218          *@return
219          *@see
220          */
221         public int precedence() {
222                 return 30;
223         }
224 
225 }
226 
227 /**
228  *  Class declaration
229  *
230  *@author
231  *@created    16. Juli 2001
232  *@version    %I%, %G%
233  */
234 class ESubtractFrom extends EAddTo {
235 
236 
237         /**
238          *  Method declaration
239          *
240          *@param  first
241          *@param  second
242          *@param  session  Description of Parameter
243          *@return
244          *@see
245          */
246         public Object execute(Object first, Object second, Session session) {
247                 return session.EV(C.Add.f((HObject) first,
248                                 C.Multiply.f(C.CN1, (HObject) second)));
249         }
250 
251 
252         /**
253          *  Method declaration
254          *
255          *@param  fun
256          *@param  session  Description of Parameter
257          *@return
258          *@see
259          */
260         public String toOpString(HFunction fun, Session session) {
261                 return E2SArg.operatorString(fun, "-=", C.SUBTRACTFROM_PRECEDENCE, session);
262         }
263 
264 }
265 
266 /**
267  *  Class declaration
268  *
269  *@author
270  *@created    16. Juli 2001
271  *@version    %I%, %G%
272  */
273 class ETimesBy extends EAddTo {
274 
275 
276         /**
277          *  Method declaration
278          *
279          *@param  first
280          *@param  second
281          *@param  session  Description of Parameter
282          *@return
283          *@see
284          */
285         public Object execute(Object first, Object second, Session session) {
286                 return session.EV(C.Multiply.f((HObject) first, (HObject) second));
287         }
288 
289 
290         /**
291          *  Method declaration
292          *
293          *@param  fun
294          *@param  session  Description of Parameter
295          *@return
296          *@see
297          */
298         public String toOpString(HFunction fun, Session session) {
299                 return E2SArg.operatorString(fun, "*=", C.TIMESBY_PRECEDENCE, session);
300         }
301 
302 }
303 
304 /**
305  *  Class declaration
306  *
307  *@author
308  *@created    16. Juli 2001
309  *@version    %I%, %G%
310  */
311 class EDivideBy extends EAddTo {
312 
313 
314         /**
315          *  Method declaration
316          *
317          *@param  first
318          *@param  second
319          *@param  session  Description of Parameter
320          *@return
321          *@see
322          */
323         public Object execute(Object first, Object second, Session session) {
324                 return session.EV(C.Multiply.f((HObject) first,
325                                 C.Pow.f((HObject) second, C.CN1)));
326         }
327 
328 
329         /**
330          *  Method declaration
331          *
332          *@param  fun
333          *@param  session  Description of Parameter
334          *@return
335          *@see
336          */
337         public String toOpString(HFunction fun, Session session) {
338                 return E2SArg.operatorString(fun, "/=", C.DIVIDEBY_PRECEDENCE, session);
339         }
340 
341 }
342 
343 /**
344  *  Description of the Class
345  *
346  *@author     khartlage
347  *@created    16. Juli 2001
348  */
349 class EIncrement extends SFunctionEvaluator implements FunctionOpEvaluator {
350 
351 
352         //, UnaryFunction
353 
354         /**
355          *  Constructor for the EIncrement object
356          */
357         public EIncrement() {
358         }
359 
360 
361         /**
362          *  Description of the Method
363          *
364          *@param  vect     Description of Parameter
365          *@param  session  Description of Parameter
366          *@return          Description of the Returned Value
367          */
368         public HObject evaluate(HFunction vect, Session session) {
369                 if (vect.size() == 1) {
370                         if (vect.get(0) instanceof HSymbol) {
371                                 HSymbol sym = (HSymbol) vect.get(0);
372                                 HObject result;
373                                 Object temp;
374 
375                                 if (!sym.hasNoLocalVar(session)) {
376                                         result = sym.deleteLocalVar(session);
377                                         temp = execute(result, session);
378 
379                                         if (temp != null) {
380                                                 sym.createLocalVar(session, (HObject) temp);
381 
382                                                 return result;
383                                         }
384 
385                                         sym.createLocalVar(session, result);
386 
387                                         return null;
388                                 }
389 
390                                 result = sym.getDownRule(sym, session);
391 
392                                 if (result != null) {
393                                         temp = execute(result, session);
394 
395                                         if (temp != null) {
396                                                 sym.putDownRule(session, HRule.SET, sym, (HObject) temp);
397 
398                                                 return result;
399                                         }
400                                 }
401                         }
402                 }
403 
404                 return null;
405         }
406 
407 
408         /**
409          *  Method declaration
410          *
411          *@param  first
412          *@param  session  Description of Parameter
413          *@return
414          *@see
415          */
416         public Object execute(Object first, Session session) {
417                 return session.EV(C.Add.f((HObject) first, C.C1));
418         }
419 
420 
421         /**
422          *  Method declaration
423          *
424          *@param  fun
425          *@param  session  Description of Parameter
426          *@return
427          *@see
428          */
429         public String toOpString(HFunction fun, Session session) {
430                 return E1SArg.operatorString(fun, "++", C.INC_DEC_PRECEDENCE, false, session);
431         }
432 
433 
434         /**
435          *  Method declaration
436          *
437          *@return
438          *@see
439          */
440         public int precedence() {
441                 return C.INC_DEC_PRECEDENCE;
442         }
443 
444 }
445 
446 /**
447  *  Description of the Class
448  *
449  *@author     khartlage
450  *@created    16. Juli 2001
451  */
452 class EDecrement extends EIncrement {
453 
454 
455         /**
456          *  Constructor for the EDecrement object
457          */
458         public EDecrement() {
459         }
460 
461 
462         /**
463          *  Method declaration
464          *
465          *@param  first
466          *@param  session  Description of Parameter
467          *@return
468          *@see
469          */
470         public Object execute(Object first, Session session) {
471                 return session.EV(C.Add.f((HObject) first, C.CN1));
472         }
473 
474 
475         /**
476          *  Method declaration
477          *
478          *@param  fun
479          *@param  session  Description of Parameter
480          *@return
481          *@see
482          */
483         public String toOpString(HFunction fun, Session session) {
484                 return E1SArg.operatorString(fun, "--", C.INC_DEC_PRECEDENCE, false, session);
485         }
486 
487 }
488 
489 /**
490  *  Description of the Class
491  *
492  *@author     khartlage
493  *@created    16. Juli 2001
494  */
495 class EPreIncrement extends SFunctionEvaluator implements FunctionOpEvaluator {
496 
497 
498         //, UnaryFunction
499 
500         /**
501          *  Constructor for the EPreIncrement object
502          */
503         public EPreIncrement() {
504         }
505 
506 
507         /**
508          *  Description of the Method
509          *
510          *@param  vect     Description of Parameter
511          *@param  session  Description of Parameter
512          *@return          Description of the Returned Value
513          */
514         public HObject evaluate(HFunction vect, Session session) {
515                 if (vect.size() == 1) {
516                         if (vect.get(0) instanceof HSymbol) {
517                                 HSymbol sym = (HSymbol) vect.get(0);
518                                 HObject result;
519                                 Object temp;
520 
521                                 if (!sym.hasNoLocalVar(session)) {
522                                         result = sym.deleteLocalVar(session);
523                                         temp = execute(result, session);
524 
525                                         if (temp != null) {
526                                                 sym.createLocalVar(session, (HObject) temp);
527 
528                                                 return (HObject) temp;
529                                         }
530 
531                                         sym.createLocalVar(session, result);
532 
533                                         return null;
534                                 }
535 
536                                 result = sym.getDownRule(sym, session);
537 
538                                 if (result != null) {
539                                         temp = execute(result, session);
540 
541                                         if (temp != null) {
542                                                 sym.putDownRule(session, HRule.SET, sym, (HObject) temp);
543 
544                                                 return (HObject) temp;
545                                         }
546                                 }
547                         }
548                 }
549 
550                 return null;
551         }
552 
553 
554         /**
555          *  Method declaration
556          *
557          *@param  first
558          *@param  session  Description of Parameter
559          *@return
560          *@see
561          */
562         public Object execute(Object first, Session session) {
563                 return session.EV(C.Add.f((HObject) first, C.C1));
564         }
565 
566 
567         /**
568          *  Method declaration
569          *
570          *@param  fun
571          *@param  session  Description of Parameter
572          *@return
573          *@see
574          */
575         public String toOpString(HFunction fun, Session session) {
576                 return E1Arg.operatorString(fun, "++", C.INC_DEC_PRECEDENCE, true, session);
577         }
578 
579 
580         /**
581          *  Method declaration
582          *
583          *@return
584          *@see
585          */
586         public int precedence() {
587                 return C.INC_DEC_PRECEDENCE;
588         }
589 
590 }
591 
592 /**
593  *  Description of the Class
594  *
595  *@author     khartlage
596  *@created    16. Juli 2001
597  */
598 class EPreDecrement extends EPreIncrement {
599 
600 
601         /**
602          *  Constructor for the EPreDecrement object
603          */
604         public EPreDecrement() {
605         }
606 
607 
608         /**
609          *  Method declaration
610          *
611          *@param  first
612          *@param  session  Description of Parameter
613          *@return
614          *@see
615          */
616         public Object execute(Object first, Session session) {
617                 return session.EV(C.Add.f((HObject) first, C.CN1));
618         }
619 
620 
621         /**
622          *  Method declaration
623          *
624          *@param  fun
625          *@param  session  Description of Parameter
626          *@return
627          *@see
628          */
629         public String toOpString(HFunction fun, Session session) {
630                 return E1Arg.operatorString(fun, "--", C.INC_DEC_PRECEDENCE, true, session);
631         }
632 
633 }
634 
635 /**
636  *  Implementation of functions for arithmetic with symbols
637  *
638  *@author     Klaus Hartlage <A HREF="mailto:khartlage@t-online.de">
639  *      khartlage@t-online.de</A>
640  *@created    16. Juli 2001
641  */
642 public class SymbolOperators implements FunctionEvaluator {
643 
644 
645         /**
646          *  Description of the Method
647          *
648          *@param  vect  Description of Parameter
649          *@return       Description of the Returned Value
650          */
651         public HObject evaluate(HFunction vect) {
652                 //   C.Complement.setEval(new EComplement());
653                 //    C.Intersection.setEval(new EIntersection());
654                 C.AddTo.setEval(new EAddTo());
655                 C.DivideBy.setEval(new EDivideBy());
656                 C.SubtractFrom.setEval(new ESubtractFrom());
657                 C.TimesBy.setEval(new ETimesBy());
658 
659                 C.Decrement.setEval(new EDecrement());
660                 C.Increment.setEval(new EIncrement());
661                 C.PreDecrement.setEval(new EPreDecrement());
662                 C.PreIncrement.setEval(new EPreIncrement());
663                 return C.Null;
664         }
665 
666 }
667