Skip to content

Commit 8c3079a

Browse files
committed
Reformat
1 parent 2d1e4f4 commit 8c3079a

File tree

116 files changed

+9031
-10369
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

116 files changed

+9031
-10369
lines changed

src/main/java/uk/co/ryanharrison/mathengine/BigRational.java

Lines changed: 1020 additions & 1142 deletions
Large diffs are not rendered by default.

src/main/java/uk/co/ryanharrison/mathengine/Utils.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ public static int matchingCharacterIndex(String expression, int index, char begi
6464
* @return source with outer parenthesis removed if present
6565
*/
6666
public static String removeOuterParenthesis(String source) {
67-
if(!source.startsWith("(")) return source;
67+
if (!source.startsWith("(")) return source;
6868
int i;
69-
while((i = Utils.matchingCharacterIndex(source, 0, '(', ')')) == source.length() - 1) {
69+
while ((i = Utils.matchingCharacterIndex(source, 0, '(', ')')) == source.length() - 1) {
7070
if (source.charAt(0) == '(' && i == source.length() - 1) {
7171
source = source.substring(1, source.length() - 1);
7272
}
@@ -81,7 +81,7 @@ public static String removeOuterParenthesis(String source) {
8181
* @return string with no spaces, trimmed and in lower case
8282
*/
8383
public static String standardiseString(String string) {
84-
if(string == null) return null;
84+
if (string == null) return null;
8585
return StringUtils.deleteWhitespace(string).toLowerCase();
8686
}
8787

src/main/java/uk/co/ryanharrison/mathengine/differential/DifferencesDirection.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
/**
44
* Represents the three commonly used forms of of differences for use when
55
* estimating derivatives numerically
6-
*
6+
* <p>
77
* The central direction tends to be the most accurate
8-
*
8+
*
99
* @author Ryan Harrison
10-
*
10+
*
1111
*/
12-
public enum DifferencesDirection
13-
{
14-
Forward, Central, Backward
12+
public enum DifferencesDirection {
13+
Forward, Central, Backward
1514
}

src/main/java/uk/co/ryanharrison/mathengine/differential/DividedDifferenceMethod.java

Lines changed: 153 additions & 169 deletions
Original file line numberDiff line numberDiff line change
@@ -5,175 +5,159 @@
55
/**
66
* {@link NumericalDifferentiationMethod} which uses the method of divided
77
* differences to estimate the derivatives.
8-
*
8+
*
99
* @author Ryan Harrison
10-
*
10+
*
1111
*/
12-
public class DividedDifferenceMethod extends NumericalDifferentiationMethod
13-
{
14-
/** The direction of the differences to use */
15-
private DifferencesDirection direction;
16-
17-
/**
18-
* Construct a new {@link DividedDifferenceMethod} instance with the
19-
* specified target {@link Function}
20-
*
21-
* This constructor will default the {@link DifferencesDirection} to Central
22-
*
23-
* @param function
24-
* The target function to numerically estimate the derivatives of
25-
*/
26-
public DividedDifferenceMethod(Function function)
27-
{
28-
this(function, DifferencesDirection.Central);
29-
}
30-
31-
/**
32-
* Construct a new {@link DividedDifferenceMethod} instance with the
33-
* specified target {@link Function} and differences direction
34-
*
35-
* This constructor will default the {@link DifferencesDirection} to Central
36-
*
37-
* @param function
38-
* The target function to numerically estimate the derivatives of
39-
* @param direction
40-
* The direction of the differences to use
41-
*/
42-
public DividedDifferenceMethod(Function function, DifferencesDirection direction)
43-
{
44-
super(function);
45-
this.direction = direction;
46-
}
47-
48-
/**
49-
* Construct a new {@link DividedDifferenceMethod} instance with the
50-
* specified target {@link Function}, difference direction and change in x
51-
*
52-
* This constructor will default the {@link DifferencesDirection} to Central
53-
*
54-
* @param function
55-
* The target function to numerically estimate the derivatives of
56-
* @param direction
57-
* The direction of the differences to use
58-
* @param h
59-
* The change in the value of x to use when numerically
60-
* estimating the derivative using finite difference
61-
* approximations. This value should be sufficiently small to
62-
* increase accuracy but large enough to prevent rounding errors
63-
*/
64-
public DividedDifferenceMethod(Function function, DifferencesDirection direction, double h)
65-
{
66-
super(function, h);
67-
this.direction = direction;
68-
}
69-
70-
/**
71-
* {@inheritDoc}
72-
*/
73-
@Override
74-
public double deriveFirst()
75-
{
76-
double x = targetPoint;
77-
double h = this.h;
78-
Function f = this.targetFunction;
79-
80-
switch (this.direction)
81-
{
82-
case Forward:
83-
return (-3 * f.evaluateAt(x) + 4 * f.evaluateAt(x + h) - f.evaluateAt(x + 2 * h))
84-
/ 2 / h;
85-
case Central:
86-
return (f.evaluateAt(x + h) - f.evaluateAt(x - h)) / 2 / h;
87-
case Backward:
88-
return (3 * f.evaluateAt(x) - 4 * f.evaluateAt(x - h) + f.evaluateAt(x - 2 * h))
89-
/ 2 / h;
90-
}
91-
92-
return x;
93-
}
94-
95-
/**
96-
* {@inheritDoc}
97-
*/
98-
@Override
99-
public double deriveFourth()
100-
{
101-
double x = targetPoint;
102-
double h = this.h;
103-
Function f = this.targetFunction;
104-
105-
switch (this.direction)
106-
{
107-
case Forward:
108-
return (3 * f.evaluateAt(x) - 14 * f.evaluateAt(x + h) + 26
109-
* f.evaluateAt(x + 2 * h) - 24 * f.evaluateAt(x + 3 * h) + 11
110-
* f.evaluateAt(x + 4 * h) - 2 * f.evaluateAt(x + 5 * h))
111-
/ h / h / h / h;
112-
case Central:
113-
return (f.evaluateAt(x - 2 * h) - 4 * f.evaluateAt(x - h) + 6 * f.evaluateAt(x) - 4
114-
* f.evaluateAt(x + h) + f.evaluateAt(x + 2 * h))
115-
/ h / h / h / h;
116-
case Backward:
117-
return (3 * f.evaluateAt(x) - 14 * f.evaluateAt(x - h) + 26
118-
* f.evaluateAt(x - 2 * h) - 24 * f.evaluateAt(x - 3 * h) + 11
119-
* f.evaluateAt(x - 4 * h) - 2 * f.evaluateAt(x - 5 * h))
120-
/ h / h / h / h;
121-
}
122-
123-
return x;
124-
}
125-
126-
/**
127-
* {@inheritDoc}
128-
*/
129-
@Override
130-
public double deriveSecond()
131-
{
132-
double x = targetPoint;
133-
double h = this.h;
134-
Function f = this.targetFunction;
135-
136-
switch (this.direction)
137-
{
138-
case Forward:
139-
return (2 * f.evaluateAt(x) - 5 * f.evaluateAt(x + h) + 4 * f.evaluateAt(x + 2 * h) - f
140-
.evaluateAt(x + 3 * h)) / h / h;
141-
case Central:
142-
return (f.evaluateAt(x - h) - 2 * f.evaluateAt(x) + f.evaluateAt(x + h)) / h / h;
143-
case Backward:
144-
return (2 * f.evaluateAt(x) - 5 * f.evaluateAt(x - h) + 4 * f.evaluateAt(x - 2 * h) - f
145-
.evaluateAt(x - 3 * h)) / h / h;
146-
}
147-
148-
return x;
149-
}
150-
151-
/**
152-
* {@inheritDoc}
153-
*/
154-
@Override
155-
public double deriveThird()
156-
{
157-
double x = targetPoint;
158-
double h = this.h;
159-
Function f = this.targetFunction;
160-
161-
switch (this.direction)
162-
{
163-
case Forward:
164-
return (-5 * f.evaluateAt(x) + 18 * f.evaluateAt(x + h) - 24
165-
* f.evaluateAt(x + 2 * h) + 14 * f.evaluateAt(x + 3 * h) - 3 * f
166-
.evaluateAt(x + 4 * h)) / 2 / h / h / h;
167-
case Central:
168-
return (-f.evaluateAt(x - 2 * h) + 2 * f.evaluateAt(x - h) - 2
169-
* f.evaluateAt(x + h) + f.evaluateAt(x + 2 * h))
170-
/ 2 / h / h / h;
171-
case Backward:
172-
return (5 * f.evaluateAt(x) - 18 * f.evaluateAt(x - h) + 24
173-
* f.evaluateAt(x - 2 * h) - 14 * f.evaluateAt(x - 3 * h) + 3 * f
174-
.evaluateAt(x - 4 * h)) / 2 / h / h / h;
175-
}
176-
177-
return x;
178-
}
12+
public class DividedDifferenceMethod extends NumericalDifferentiationMethod {
13+
/**
14+
* The direction of the differences to use
15+
*/
16+
private DifferencesDirection direction;
17+
18+
/**
19+
* Construct a new {@link DividedDifferenceMethod} instance with the
20+
* specified target {@link Function}
21+
* <p>
22+
* This constructor will default the {@link DifferencesDirection} to Central
23+
*
24+
* @param function The target function to numerically estimate the derivatives of
25+
*/
26+
public DividedDifferenceMethod(Function function) {
27+
this(function, DifferencesDirection.Central);
28+
}
29+
30+
/**
31+
* Construct a new {@link DividedDifferenceMethod} instance with the
32+
* specified target {@link Function} and differences direction
33+
* <p>
34+
* This constructor will default the {@link DifferencesDirection} to Central
35+
*
36+
* @param function The target function to numerically estimate the derivatives of
37+
* @param direction The direction of the differences to use
38+
*/
39+
public DividedDifferenceMethod(Function function, DifferencesDirection direction) {
40+
super(function);
41+
this.direction = direction;
42+
}
43+
44+
/**
45+
* Construct a new {@link DividedDifferenceMethod} instance with the
46+
* specified target {@link Function}, difference direction and change in x
47+
* <p>
48+
* This constructor will default the {@link DifferencesDirection} to Central
49+
*
50+
* @param function The target function to numerically estimate the derivatives of
51+
* @param direction The direction of the differences to use
52+
* @param h The change in the value of x to use when numerically
53+
* estimating the derivative using finite difference
54+
* approximations. This value should be sufficiently small to
55+
* increase accuracy but large enough to prevent rounding errors
56+
*/
57+
public DividedDifferenceMethod(Function function, DifferencesDirection direction, double h) {
58+
super(function, h);
59+
this.direction = direction;
60+
}
61+
62+
/**
63+
* {@inheritDoc}
64+
*/
65+
@Override
66+
public double deriveFirst() {
67+
double x = targetPoint;
68+
double h = this.h;
69+
Function f = this.targetFunction;
70+
71+
switch (this.direction) {
72+
case Forward:
73+
return (-3 * f.evaluateAt(x) + 4 * f.evaluateAt(x + h) - f.evaluateAt(x + 2 * h))
74+
/ 2 / h;
75+
case Central:
76+
return (f.evaluateAt(x + h) - f.evaluateAt(x - h)) / 2 / h;
77+
case Backward:
78+
return (3 * f.evaluateAt(x) - 4 * f.evaluateAt(x - h) + f.evaluateAt(x - 2 * h))
79+
/ 2 / h;
80+
}
81+
82+
return x;
83+
}
84+
85+
/**
86+
* {@inheritDoc}
87+
*/
88+
@Override
89+
public double deriveFourth() {
90+
double x = targetPoint;
91+
double h = this.h;
92+
Function f = this.targetFunction;
93+
94+
switch (this.direction) {
95+
case Forward:
96+
return (3 * f.evaluateAt(x) - 14 * f.evaluateAt(x + h) + 26
97+
* f.evaluateAt(x + 2 * h) - 24 * f.evaluateAt(x + 3 * h) + 11
98+
* f.evaluateAt(x + 4 * h) - 2 * f.evaluateAt(x + 5 * h))
99+
/ h / h / h / h;
100+
case Central:
101+
return (f.evaluateAt(x - 2 * h) - 4 * f.evaluateAt(x - h) + 6 * f.evaluateAt(x) - 4
102+
* f.evaluateAt(x + h) + f.evaluateAt(x + 2 * h))
103+
/ h / h / h / h;
104+
case Backward:
105+
return (3 * f.evaluateAt(x) - 14 * f.evaluateAt(x - h) + 26
106+
* f.evaluateAt(x - 2 * h) - 24 * f.evaluateAt(x - 3 * h) + 11
107+
* f.evaluateAt(x - 4 * h) - 2 * f.evaluateAt(x - 5 * h))
108+
/ h / h / h / h;
109+
}
110+
111+
return x;
112+
}
113+
114+
/**
115+
* {@inheritDoc}
116+
*/
117+
@Override
118+
public double deriveSecond() {
119+
double x = targetPoint;
120+
double h = this.h;
121+
Function f = this.targetFunction;
122+
123+
switch (this.direction) {
124+
case Forward:
125+
return (2 * f.evaluateAt(x) - 5 * f.evaluateAt(x + h) + 4 * f.evaluateAt(x + 2 * h) - f
126+
.evaluateAt(x + 3 * h)) / h / h;
127+
case Central:
128+
return (f.evaluateAt(x - h) - 2 * f.evaluateAt(x) + f.evaluateAt(x + h)) / h / h;
129+
case Backward:
130+
return (2 * f.evaluateAt(x) - 5 * f.evaluateAt(x - h) + 4 * f.evaluateAt(x - 2 * h) - f
131+
.evaluateAt(x - 3 * h)) / h / h;
132+
}
133+
134+
return x;
135+
}
136+
137+
/**
138+
* {@inheritDoc}
139+
*/
140+
@Override
141+
public double deriveThird() {
142+
double x = targetPoint;
143+
double h = this.h;
144+
Function f = this.targetFunction;
145+
146+
switch (this.direction) {
147+
case Forward:
148+
return (-5 * f.evaluateAt(x) + 18 * f.evaluateAt(x + h) - 24
149+
* f.evaluateAt(x + 2 * h) + 14 * f.evaluateAt(x + 3 * h) - 3 * f
150+
.evaluateAt(x + 4 * h)) / 2 / h / h / h;
151+
case Central:
152+
return (-f.evaluateAt(x - 2 * h) + 2 * f.evaluateAt(x - h) - 2
153+
* f.evaluateAt(x + h) + f.evaluateAt(x + 2 * h))
154+
/ 2 / h / h / h;
155+
case Backward:
156+
return (5 * f.evaluateAt(x) - 18 * f.evaluateAt(x - h) + 24
157+
* f.evaluateAt(x - 2 * h) - 14 * f.evaluateAt(x - 3 * h) + 3 * f
158+
.evaluateAt(x - 4 * h)) / 2 / h / h / h;
159+
}
160+
161+
return x;
162+
}
179163
}

0 commit comments

Comments
 (0)