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 /// 33 T euler(T)( in T x, T delegate(in T,double) f, double time, double h ) 34 if( hasBasicMathOp!T ) 35 { 36 return x + f( x, time ) * h; 37 } 38 39 /// 40 T runge(T)( in T x, T delegate(in T,double) f, double time, double h ) 41 if( hasBasicMathOp!T ) 42 { 43 T k1 = f( x, time ) * h; 44 T k2 = f( x + k1 * 0.5, time + h * 0.5 ) * h; 45 T k3 = f( x + k2 * 0.5, time + h * 0.5 ) * h; 46 T k4 = f( x + k3, time + h ) * h; 47 return cast(T)( x + ( k1 + k2 * 2.0 + k3 * 2.0 + k4 ) / 6.0 ); 48 } 49 50 unittest 51 { 52 double a1 = 0, a2 = 0, pa = 5; 53 double time = 0, ft = 10, step = .01; 54 55 auto rpart( in double A, double time ) { return pa; } 56 57 foreach( i; 0 .. cast(ulong)(ft/step) ) 58 { 59 a1 = euler( a1, &rpart, time+=step, step ); 60 a2 = runge( a1, &rpart, time+=step, step ); 61 } 62 63 import std.math; 64 auto va = ft * pa; 65 assert( abs( va - a1 ) <= step * 2 * pa ); 66 assert( abs( va - a2 ) <= step * pa ); 67 68 auto rpart2( in float A, double time ) { return pa; } 69 70 static assert( !is(typeof( euler( a1, &rpart2, 0, 0 ) ))); 71 } 72 73 unittest 74 { 75 static struct Pos 76 { 77 double x=0, y=0; 78 mixin( BasicMathOp!"x y" ); 79 } 80 81 static struct Point 82 { 83 Pos pos, vel; 84 mixin( BasicMathOp!"pos vel" ); 85 } 86 87 Pos acc( in Pos p ) 88 { 89 return Pos( -(p.x * abs(p.x)), -(p.y * abs(p.y)) ); 90 } 91 92 Point rpart( in Point p, double time ) 93 { 94 return Point( p.vel, acc(p.pos) ); 95 } 96 97 auto state1 = Point( Pos(50,10), Pos(5,15) ); 98 auto state2 = Point( Pos(50,10), Pos(5,15) ); 99 100 double t = 0, ft = 10, dt = 0.01; 101 102 foreach( i; 0 .. cast(size_t)(ft/dt) ) 103 { 104 state1 = euler( state1, &rpart, t+=dt, dt ); 105 state2 = runge( state2, &rpart, t+=dt, dt ); 106 } 107 } 108 109 /// 110 unittest 111 { 112 import des.math.linear.vector; 113 114 static struct Point 115 { 116 vec3 pos, vel; 117 mixin( BasicMathOp!"pos vel" ); 118 } 119 120 auto v1 = Point( vec3(10,3,1), vec3(5,4,3) ); 121 auto v2 = Point( vec3(10,3,1), vec3(5,4,3) ); 122 assert( v1 + v2 == Point( vec3(20,6,2), vec3(10,8,6) ) ); 123 assert( v1 * 2.0 == Point( vec3(20,6,2), vec3(10,8,6) ) ); 124 125 Point rpart( in Point p, double time ) 126 { return Point( p.vel, vec3(0,0,0) ); } 127 128 auto v = Point( vec3(10,3,1), vec3(5,4,3) ); 129 130 double time = 0, ft = 10, step = .01; 131 foreach( i; 0 .. cast(ulong)(ft/step+1) ) 132 v = runge( v, &rpart, time+=step, step ); 133 134 assert( (v.pos - vec3(60,43,31)).len2 < 1e-5 ); 135 }