1 /+
2 The MIT License (MIT)
3 
4     Copyright (c) <2013> <Oleg Butko (deviator), Anton Akzhigitov (Akzwar)>
5 
6     Permission is hereby granted, free of charge, to any person obtaining a copy
7     of this software and associated documentation files (the "Software"), to deal
8     in the Software without restriction, including without limitation the rights
9     to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10     copies of the Software, and to permit persons to whom the Software is
11     furnished to do so, subject to the following conditions:
12 
13     The above copyright notice and this permission notice shall be included in
14     all copies or substantial portions of the Software.
15 
16     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22     THE SOFTWARE.
23 +/
24 
25 module des.math.method.calculus.integ;
26 
27 public import des.math.basic.traits;
28 public import des.math.basic.mathstruct;
29 
30 import std.math;
31 
32 T euler(T)( in T x, T delegate(in T,double) f, double time, double h )
33     if( hasBasicMathOp!T )
34 {
35     return x + f( x, time ) * h;
36 }
37 
38 T runge(T)( in T x, T delegate(in T,double) f, double time, double h )
39     if( hasBasicMathOp!T )
40 {
41     T k1 = f( x, time ) * h;
42     T k2 = f( x + k1 * 0.5, time + h * 0.5 ) * h;
43     T k3 = f( x + k2 * 0.5, time + h * 0.5 ) * h;
44     T k4 = f( x + k3, time + h ) * h;
45     return cast(T)( x + ( k1 + k2 * 2.0 + k3 * 2.0 + k4 ) / 6.0 );
46 }
47 
48 unittest
49 {
50     double a1 = 0, a2 = 0, pa = 5;
51     double time = 0, ft = 10, step = .01;
52 
53     auto rpart( in double A, double time ) { return pa; }
54 
55     foreach( i; 0 .. cast(ulong)(ft/step) )
56     {
57         a1 = euler( a1, &rpart, time+=step, step );
58         a2 = runge( a1, &rpart, time+=step, step );
59     }
60 
61     import std.math;
62     auto va = ft * pa;
63     assert( abs( va - a1 ) <= step * 2 * pa );
64     assert( abs( va - a2 ) <= step * pa );
65 
66     auto rpart2( in float A, double time ) { return pa; }
67 
68     static assert( !is(typeof( euler( a1, &rpart2, 0, 0 ) )));
69 }
70 
71 unittest
72 {
73     static struct Pos
74     {
75         double x=0, y=0;
76         mixin( BasicMathOp!"x y" );
77     }
78 
79     static struct Point
80     {
81         Pos pos, vel;
82         mixin( BasicMathOp!"pos vel" );
83     }
84 
85     Pos acc( in Pos p )
86     {
87         return Pos( -(p.x * abs(p.x)), -(p.y * abs(p.y)) );
88     }
89 
90     Point rpart( in Point p, double time )
91     {
92         return Point( p.vel, acc(p.pos) );
93     }
94 
95     auto state1 = Point( Pos(50,10), Pos(5,15) );
96     auto state2 = Point( Pos(50,10), Pos(5,15) );
97 
98     double t = 0, ft = 10, dt = 0.01;
99 
100     foreach( i; 0 .. cast(size_t)(ft/dt) )
101     {
102         state1 = euler( state1, &rpart, t+=dt, dt );
103         state2 = runge( state2, &rpart, t+=dt, dt );
104     }
105 }
106 
107 unittest
108 {
109     import des.math.linear.vector;
110 
111     static struct Point 
112     { 
113         vec3 pos, vel; 
114         mixin( BasicMathOp!"pos vel" );
115     }
116 
117     auto v1 = Point( vec3(10,3,1), vec3(5,4,3) );
118     auto v2 = Point( vec3(10,3,1), vec3(5,4,3) );
119     assert( v1 + v2 == Point( vec3(20,6,2), vec3(10,8,6) ) );
120     assert( v1 * 2.0 == Point( vec3(20,6,2), vec3(10,8,6) ) );
121 
122     Point rpart( in Point p, double time )
123     { return Point( p.vel, vec3(0,0,0) ); }
124 
125     auto v = Point( vec3(10,3,1), vec3(5,4,3) );
126 
127     double time = 0, ft = 10, step = .01;
128     foreach( i; 0 .. cast(ulong)(ft/step+1) )
129         v = runge( v, &rpart, time+=step, step );
130 
131     assert( (v.pos - vec3(60,43,31)).len2 < 1e-5 );
132 }