1 /* 2 * Copyright (C) 2012 Alberto Irurueta Carro (alberto@irurueta.com) 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.irurueta.numerical; 17 18 /** 19 * This class evaluates a multidimensional function along a line, such line is 20 * defined by an input point and a given direction. 21 * Using provided input point and direction, the multidimensional function's 22 * input parameters are determined, so they all lay on a line. 23 */ 24 public class DirectionalEvaluator { 25 26 /** 27 * Listener to evaluate a multidimensional function. 28 */ 29 protected MultiDimensionFunctionEvaluatorListener listener; 30 31 /** 32 * Point used as a reference to determine the function's input parameters 33 * along a line. 34 */ 35 protected double[] point; 36 37 /** 38 * Vector indicating the direction of the line where the function is 39 * evaluated. 40 */ 41 protected double[] direction; 42 43 /** 44 * Point currently being evaluated in the multidimensional function. 45 * This is used internally. 46 */ 47 protected final double[] p; 48 49 /** 50 * Constructor. 51 * 52 * @param listener Listener to evaluate a multidimensional function. 53 * @param point Point used as a reference to determine the function's input 54 * parameters along a line. 55 * @param direction Vector indicating the direction of the line where the 56 * function is evaluated. 57 * @throws IllegalArgumentException Raised if point and direction don't have 58 * the same length. 59 */ 60 public DirectionalEvaluator( 61 final MultiDimensionFunctionEvaluatorListener listener, final double[] point, final double[] direction) { 62 63 setPointAndDirection(point, direction); 64 this.listener = listener; 65 p = new double[point.length]; 66 } 67 68 /** 69 * Returns listener to evaluate a multidimensional function. 70 * 71 * @return Listener to evaluate a multidimensional function. 72 */ 73 public MultiDimensionFunctionEvaluatorListener getListener() { 74 return listener; 75 } 76 77 /** 78 * Sets listener to evaluate a multidimensional function. 79 * 80 * @param listener Listener to evaluate a multidimensional function. 81 */ 82 public void setListener(final MultiDimensionFunctionEvaluatorListener listener) { 83 this.listener = listener; 84 } 85 86 /** 87 * Returns point used as a reference to determine the function's input 88 * parameters along a line. 89 * 90 * @return Point used as a reference to determine the function's input 91 * parameters along a line 92 */ 93 public double[] getPoint() { 94 return point; 95 } 96 97 /** 98 * Returns array indicating the direction of the line where the function is 99 * evaluated. 100 * 101 * @return Array indicating the direction of the line where the function is 102 * evaluated. 103 */ 104 public double[] getDirection() { 105 return direction; 106 } 107 108 /** 109 * Sets point used as a reference to determine the function's input 110 * parameters along a line and the direction of the line where the 111 * function is evaluated. 112 * 113 * @param point Point used as a reference to determine the function's input 114 * parameters along a line. 115 * @param direction Array indicating the direction of the line where the 116 * function is evaluated. 117 * @throws IllegalArgumentException Raised if point and direction don't have 118 * the same length. 119 */ 120 public final void setPointAndDirection(final double[] point, final double[] direction) { 121 if (point.length != direction.length) { 122 throw new IllegalArgumentException(); 123 } 124 125 this.point = point; 126 this.direction = direction; 127 } 128 129 /** 130 * Evaluates a function using current listener at a distance x from current 131 * point using current direction 132 * 133 * @param x Distance from provided point using provided direction where 134 * function is being evaluated 135 * @return Result of evaluating the function 136 * @throws EvaluationException Thrown if function evaluation fails 137 */ 138 public double evaluateAt(final double x) throws EvaluationException { 139 for (var i = 0; i < point.length; i++) { 140 p[i] = point[i] + x * direction[i]; 141 } 142 return listener.evaluate(p); 143 } 144 }