Source code: exptree/operators/invert.java
1 /* Evolvo - Image Generator
2 * Copyright (C) 2000 Andrew Molloy
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 /*
20 * @(#)invert.java 0.1 08/19/2000
21 */
22 package exptree.operators;
23
24 import java.io.*;
25 import exptree.*;
26
27 /**
28 * Inverts the value of an expressionTree (for instance 1/x).
29 *
30 * This particular application expects all inputs and outputs on the expressionTree
31 * to be bound to [-1.0,1.0], so this function is not actually natural inversion.
32 *
33 * @version 1.1 08/19/2000
34 * @author Andy Molloy
35 */
36 public class invert implements operatorInterface, Serializable
37 {
38
39 /** Perform the operation. */
40 public double evaluate(expressionTree params[])
41 {
42 double temp = params[0].evaluate();
43 double result;
44
45 try
46 {
47 result = 1.0 / temp;
48 }
49 catch (ArithmeticException ae) // Division by zero
50 { // +/- infinity. We just set it to the extreme of our range
51 if ( Math.abs(temp) != temp ) // It's -infinity
52 {
53 result = Double.NEGATIVE_INFINITY;
54 }
55 else
56 {
57 result = Double.POSITIVE_INFINITY;
58 }
59 }
60
61 return result;
62 }
63
64 /** Returns the operator's name. */
65 public String getName()
66 {
67 return "invert";
68 }
69
70 /** Performs any initialization the operator requires. */
71 public void init() {}
72
73 /** Returns the number of parameters expected by the operator. */
74 public int getNumberOfParameters() { return 1; }
75 }