source: src/main/java/negotiator/boaframework/offeringstrategy/anac2013/TheFawkes/SmoothingPolynomial.java

Last change on this file was 127, checked in by Wouter Pasman, 6 years ago

#41 ROLL BACK of rev.126 . So this version is equal to rev. 125

File size: 4.8 KB
Line 
1package negotiator.boaframework.offeringstrategy.anac2013.TheFawkes;
2
3/*
4 * Class: Polynomial
5 * Description:
6 * Environment: Java
7 * Software: SSJ
8 * Copyright (C) 2001 Pierre L'Ecuyer and Université de Montréal
9 * Organization: DIRO, Université de Montréal
10 * @author Éric Buist
11
12 * SSJ is free software: you can redistribute it and/or modify it under
13 * the terms of the GNU General Public License (GPL) as published by the
14 * Free Software Foundation, either version 3 of the License, or
15 * any later version.
16
17 * SSJ is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21
22 * A copy of the GNU General Public License is available at
23 <a href="http://www.gnu.org/licenses">GPL licence site</a>.
24 */
25/**
26 * Represents a polynomial of degree <SPAN CLASS="MATH"><I>n</I></SPAN> in power form. Such a polynomial is of the form
27 *
28 * <P></P> <DIV ALIGN="CENTER" CLASS="mathdisplay"> <I>p</I>(<I>x</I>) = <I>c</I><SUB>0</SUB> +
29 * <I>c</I><SUB>1</SUB><I>x</I> + <SUP> ... </SUP> + <I>c</I><SUB>n</SUB><I>x</I><SUP>n</SUP>, </DIV><P></P> where <SPAN
30 * CLASS="MATH"><I>c</I><SUB>0</SUB>,&#8230;, <I>c</I><SUB>n</SUB></SPAN> are the coefficients of the polynomial.
31 *
32 */
33public final class SmoothingPolynomial
34{
35 private double[] coeff;
36
37 /**
38 * Constructs a new polynomial with coefficients <TT>coeff</TT>. The value of <TT>coeff[i]</TT> in this array
39 * corresponds to <SPAN CLASS="MATH"><I>c</I><SUB>i</SUB></SPAN>.
40 *
41 * @param coeff the coefficients of the polynomial.
42 *
43 * @exception NullPointerException if <TT>coeff</TT> is <TT>null</TT>.
44 *
45 * @exception IllegalArgumentException if the length of <TT>coeff</TT> is 0.
46 *
47 */
48 public SmoothingPolynomial( double... coeff )
49 {
50 if( coeff == null )
51 {
52 throw new NullPointerException();
53 }
54 else if( coeff.length == 0 )
55 {
56 throw new IllegalArgumentException( "At least one coefficient is needed" );
57 }
58 else
59 {
60 this.coeff = coeff.clone();
61 }
62 }
63
64 /**
65 * Returns the <SPAN CLASS="MATH"><I>i</I></SPAN>th coefficient of the polynomial.
66 *
67 * @return the array of coefficients.
68 *
69 */
70 public double getCoefficient( int i )
71 {
72 return this.coeff[i];
73 }
74
75 /**
76 * Sets the array of coefficients of this polynomial to <TT>coeff</TT>.
77 *
78 * @param coeff the new array of coefficients.
79 *
80 * @exception NullPointerException if <TT>coeff</TT> is <TT>null</TT>.
81 *
82 * @exception IllegalArgumentException if the length of <TT>coeff</TT> is 0.
83 *
84 */
85 public void setCoefficients( double... coeff )
86 {
87 if( coeff == null )
88 {
89 throw new NullPointerException();
90 }
91 else if( coeff.length == 0 )
92 {
93 throw new IllegalArgumentException( "At least one coefficient is needed" );
94 }
95 else
96 {
97 this.coeff = coeff.clone();
98 }
99 }
100
101 public double evaluate( double x )
102 {
103 double res = this.coeff[this.coeff.length - 1];
104 for( int i = ( this.coeff.length - 2 ); i >= 0; i-- )
105 {
106 res = this.coeff[i] + x * res;
107 }
108 return res;
109 }
110
111 private double getCoeffDer( int i, int n )
112 {
113 double coeffDer = this.coeff[i];
114 for( int j = i; j > ( i - n ); j-- )
115 {
116 coeffDer *= j;
117 }
118 return coeffDer;
119 }
120
121 public double derivative( double x )
122 {
123 return this.derivative( x, 1 );
124 }
125
126 public double derivative( double x, int n )
127 {
128 if( n < 0 )
129 {
130 throw new IllegalArgumentException( "n < 0" );
131 }
132 else if( n == 0 )
133 {
134 return this.evaluate( x );
135 }
136 else if( n >= coeff.length )
137 {
138 return 0;
139 }
140 else
141 {
142 double res = this.getCoeffDer( this.coeff.length - 1, n );
143 for( int i = ( this.coeff.length - 2 ); i >= n; i-- )
144 {
145 res = this.getCoeffDer( i, n ) + ( x * res );
146 }
147 return res;
148 }
149 }
150
151 @Override
152 public SmoothingPolynomial clone()
153 {
154 try
155 {
156 SmoothingPolynomial pol = (SmoothingPolynomial)super.clone();
157 pol.setCoefficients( this.coeff );
158 return pol;
159 }
160 catch( CloneNotSupportedException cne )
161 {
162 throw new IllegalStateException( "Clone not supported (" + cne.getMessage() + ")" );
163 }
164 }
165}
Note: See TracBrowser for help on using the repository browser.