1 /*
2 * jcurl java curling software framework https://JCurl.mro.name
3 * Copyright (C) 2005-2009 M. Rohrmoser
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13 * Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 package org.jcurl.math;
20
21 /**
22 * Polynomes of n-th grade.
23 *
24 * @author <a href="mailto:JCurl@mro.name">M. Rohrmoser </a>
25 * @version $Id: Polynome.java 1031 2009-07-23 15:06:05Z mro $
26 */
27 public class Polynome extends R1R1Function {
28
29 private static final long serialVersionUID = 7503158531478359260L;
30
31 /**
32 * Compute <code>a!</code>
33 *
34 * @param a
35 * @return a!
36 */
37 static long fak(final int a) {
38 return fak(a, 1);
39 }
40
41 /**
42 * Compute <code>high! / low!</code>
43 *
44 * @param high
45 * @param low
46 * @return high! / low!
47 */
48 static long fak(final int high, final int low) {
49 if (high < 2)
50 return 1;
51 long ret = 1;
52 for (int i = low < 2 ? 1 : low; i < high; ret *= ++i)
53 ;
54 return ret;
55 }
56
57 /**
58 * Convenience method to get the "bewegungsgleichung" for a given initial
59 * state.
60 *
61 * @param t0
62 * initial time
63 * @param x0
64 * initial location
65 * @param v0
66 * initial speed
67 * @param a0
68 * constant acceleration
69 * @return the resulting polynome
70 * @see #getPolyParams(double, double, double, double)
71 */
72 public static final Polynome getPoly(final double t0, final double x0,
73 final double v0, final double a0) {
74 return new Polynome(getPolyParams(t0, x0, v0, a0));
75 }
76
77 /**
78 * Convenience method to get the "bewegungsgleichung" for a given initial
79 * state.
80 *
81 * @param t0
82 * initial time
83 * @param x0
84 * initial location
85 * @param v0
86 * initial speed
87 * @param a0
88 * constant acceleration
89 * @return the resulting polynome's parameters
90 */
91 public static final double[] getPolyParams(final double t0,
92 final double x0, final double v0, final double a0) {
93 final double[] p = { x0 - v0 * t0 + 0.5 * a0 * t0 * t0, v0 - a0 * t0,
94 0.5 * a0 };
95 return p;
96 }
97
98 /**
99 * Compute the polynome p at x.
100 *
101 * @param x
102 * @param p
103 * polynome coefficients
104 * @return <code>p(x)</code>
105 * @see #poly(double, int, double[])
106 */
107 public static double poly(final double x, final double[] p) {
108 return poly(x, 0, p);
109 }
110
111 /**
112 * Compute the c-th derivative of the polynome p at x.
113 *
114 * @param x
115 * @param c
116 * derivative
117 * @param p
118 * polynome coefficients
119 *
120 * @return <code>d/dx^c p(x)</code>
121 */
122 public static double poly(final double x, final int c, final double[] p) {
123 double ret = 0;
124 for (int i = p.length - 1; i >= c; i--) {
125 ret *= x;
126 ret += fak(i, i - c) * p[i];
127 }
128 return ret;
129 }
130
131 public static String toString(final double[] poly) {
132 final StringBuilder ret = new StringBuilder();
133 ret.append("p(x) = ");
134 for (int i = 0; i < poly.length; i++) {
135 ret.append(poly[i]);
136 ret.append("*x**");
137 ret.append(i);
138 ret.append(" + ");
139 }
140 ret.setLength(ret.length() - 3);
141 return ret.toString();
142 }
143
144 final double[] params;
145
146 public Polynome(final double[] params) {
147 this.params = params;
148 }
149
150 /**
151 * @param x
152 * @param c
153 * @return the value
154 * @see #poly(double, int, double[])
155 */
156 @Override
157 public double at(final double x, final int c) {
158 return poly(x, c, params);
159 }
160
161 @Override
162 public String toString() {
163 return toString(params);
164 }
165 }