| 1 | |||||||||||||||
| 2 | =encoding utf8 | ||||||||||||||
| 3 | |||||||||||||||
| 4 | =head1 NAME | ||||||||||||||
| 5 | |||||||||||||||
| 6 | Math::Symbolic::VectorCalculus - Symbolically comp. grad, Jacobi matrices etc. | ||||||||||||||
| 7 | |||||||||||||||
| 8 | =head1 SYNOPSIS | ||||||||||||||
| 9 | |||||||||||||||
| 10 | use Math::Symbolic qw/:all/; | ||||||||||||||
| 11 | use Math::Symbolic::VectorCalculus; # not loaded by Math::Symbolic | ||||||||||||||
| 12 | |||||||||||||||
| 13 | @gradient = grad 'x+y*z'; | ||||||||||||||
| 14 | # or: | ||||||||||||||
| 15 | $function = parse_from_string('a*b^c'); | ||||||||||||||
| 16 | @gradient = grad $function; | ||||||||||||||
| 17 | # or: | ||||||||||||||
| 18 | @signature = qw(x y z); | ||||||||||||||
| 19 | @gradient = grad 'a*x+b*y+c*z', @signature; # Gradient only for x, y, z | ||||||||||||||
| 20 | # or: | ||||||||||||||
| 21 | @gradient = grad $function, @signature; | ||||||||||||||
| 22 | |||||||||||||||
| 23 | # Similar syntax variations as with the gradient: | ||||||||||||||
| 24 | $divergence = div @functions; | ||||||||||||||
| 25 | $divergence = div @functions, @signature; | ||||||||||||||
| 26 | |||||||||||||||
| 27 | # Again, similar DWIM syntax variations as with grad: | ||||||||||||||
| 28 | @rotation = rot @functions; | ||||||||||||||
| 29 | @rotation = rot @functions, @signature; | ||||||||||||||
| 30 | |||||||||||||||
| 31 | # Signatures always inferred from the functions here: | ||||||||||||||
| 32 | @matrix = Jacobi @functions; | ||||||||||||||
| 33 | # $matrix is now array of array references. These hold | ||||||||||||||
| 34 | # Math::Symbolic trees. Or: | ||||||||||||||
| 35 | @matrix = Jacobi @functions, @signature; | ||||||||||||||
| 36 | |||||||||||||||
| 37 | # Similar to Jacobi: | ||||||||||||||
| 38 | @matrix = Hesse $function; | ||||||||||||||
| 39 | # or: | ||||||||||||||
| 40 | @matrix = Hesse $function, @signature; | ||||||||||||||
| 41 | |||||||||||||||
| 42 | $wronsky_determinant = WronskyDet @functions, @vars; | ||||||||||||||
| 43 | # or: | ||||||||||||||
| 44 | $wronsky_determinant = WronskyDet @functions; # functions of 1 variable | ||||||||||||||
| 45 | |||||||||||||||
| 46 | $differential = TotalDifferential $function; | ||||||||||||||
| 47 | $differential = TotalDifferential $function, @signature; | ||||||||||||||
| 48 | $differential = TotalDifferential $function, @signature, @point; | ||||||||||||||
| 49 | |||||||||||||||
| 50 | $dir_deriv = DirectionalDerivative $function, @vector; | ||||||||||||||
| 51 | $dir_deriv = DirectionalDerivative $function, @vector, @signature; | ||||||||||||||
| 52 | |||||||||||||||
| 53 | $taylor = TaylorPolyTwoDim $function, $var1, $var2, $degree; | ||||||||||||||
| 54 | $taylor = TaylorPolyTwoDim $function, $var1, $var2, | ||||||||||||||
| 55 | $degree, $var1_0, $var2_0; | ||||||||||||||
| 56 | # example: | ||||||||||||||
| 57 | $taylor = TaylorPolyTwoDim 'sin(x)*cos(y)', 'x', 'y', 2; | ||||||||||||||
| 58 | |||||||||||||||
| 59 | =head1 DESCRIPTION | ||||||||||||||
| 60 | |||||||||||||||
| 61 | This module provides several subroutines related to | ||||||||||||||
| 62 | vector calculus such as computing gradients, divergence, rotation, | ||||||||||||||
| 63 | and Jacobi/Hesse Matrices of Math::Symbolic trees. | ||||||||||||||
| 64 | Furthermore it provides means of computing directional derivatives | ||||||||||||||
| 65 | and the total differential of a scalar function and the | ||||||||||||||
| 66 | Wronsky Determinant of a set of n scalar functions. | ||||||||||||||
| 67 | |||||||||||||||
| 68 | Please note that the code herein may or may not be refactored into | ||||||||||||||
| 69 | the OO-interface of the Math::Symbolic module in the future. | ||||||||||||||
| 70 | |||||||||||||||
| 71 | =head2 EXPORT | ||||||||||||||
| 72 | |||||||||||||||
| 73 | None by default. | ||||||||||||||
| 74 | |||||||||||||||
| 75 | You may choose to have any of the following routines exported to the | ||||||||||||||
| 76 | calling namespace. ':all' tag exports all of the following: | ||||||||||||||
| 77 | |||||||||||||||
| 78 | grad | ||||||||||||||
| 79 | div | ||||||||||||||
| 80 | rot | ||||||||||||||
| 81 | Jacobi | ||||||||||||||
| 82 | Hesse | ||||||||||||||
| 83 | WronskyDet | ||||||||||||||
| 84 | TotalDifferential | ||||||||||||||
| 85 | DirectionalDerivative | ||||||||||||||
| 86 | TaylorPolyTwoDim | ||||||||||||||
| 87 | |||||||||||||||
| 88 | =head1 SUBROUTINES | ||||||||||||||
| 89 | |||||||||||||||
| 90 | =cut | ||||||||||||||
| 91 | |||||||||||||||
| 92 | package Math::Symbolic::VectorCalculus; | ||||||||||||||
| 93 | |||||||||||||||
| 94 | 2 | ▶ | use 5.006; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 95 | 2 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 96 | 2 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 97 | |||||||||||||||
| 98 | 2 | ▶ | use Carp; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 99 | |||||||||||||||
| 100 | 2 | ▶ | use Math::Symbolic qw/:all/; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 101 | 2 | ▶ | use Math::Symbolic::MiscAlgebra qw/det/; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 102 | |||||||||||||||
| 103 | 2 | require Exporter; | |||||||||||||
| 104 | 2 | our @ISA = qw(Exporter); | |||||||||||||
| 105 | 2 | our %EXPORT_TAGS = ( | |||||||||||||
| 106 | 'all' => [ | ||||||||||||||
| 107 | qw( | ||||||||||||||
| 108 | grad | ||||||||||||||
| 109 | div | ||||||||||||||
| 110 | rot | ||||||||||||||
| 111 | Jacobi | ||||||||||||||
| 112 | Hesse | ||||||||||||||
| 113 | TotalDifferential | ||||||||||||||
| 114 | DirectionalDerivative | ||||||||||||||
| 115 | TaylorPolyTwoDim | ||||||||||||||
| 116 | WronskyDet | ||||||||||||||
| 117 | ) | ||||||||||||||
| 118 | ] | ||||||||||||||
| 119 | ); | ||||||||||||||
| 120 | |||||||||||||||
| 121 | 2 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | |||||||||||||
| 122 | |||||||||||||||
| 123 | 2 | our $VERSION = '0.613'; | |||||||||||||
| 124 | |||||||||||||||
| 125 | =begin comment | ||||||||||||||
| 126 | |||||||||||||||
| 127 | _combined_signature returns the combined signature of unique variable names | ||||||||||||||
| 128 | of all Math::Symbolic trees passed to it. | ||||||||||||||
| 129 | |||||||||||||||
| 130 | =end comment | ||||||||||||||
| 131 | |||||||||||||||
| 132 | =cut | ||||||||||||||
| 133 | |||||||||||||||
| 134 | 4 | ▶ | sub _combined_signature { | ||||||||||||
|
Subroutine
_combined_signature: called CC=1 SCAR=0.0
| |||||||||||||||
| 135 | 4 | my %seen = map { ( $_, undef ) } map { ( $_->signature() ) } @_; | |||||||||||||
| 136 | 4 | return [ sort keys %seen ]; | |||||||||||||
| 137 | } | ||||||||||||||
| 138 | |||||||||||||||
| 139 | =head2 grad | ||||||||||||||
| 140 | |||||||||||||||
| 141 | This subroutine computes the gradient of a Math::Symbolic tree representing | ||||||||||||||
| 142 | a function. | ||||||||||||||
| 143 | |||||||||||||||
| 144 | The gradient of a function f(x1, x2, ..., xn) is defined as the vector: | ||||||||||||||
| 145 | |||||||||||||||
| 146 | ( df(x1, x2, ..., xn) / d(x1), | ||||||||||||||
| 147 | df(x1, x2, ..., xn) / d(x2), | ||||||||||||||
| 148 | ..., | ||||||||||||||
| 149 | df(x1, x2, ..., xn) / d(xn) ) | ||||||||||||||
| 150 | |||||||||||||||
| 151 | (These are all partial derivatives.) Any good book on calculus will have | ||||||||||||||
| 152 | more details on this. | ||||||||||||||
| 153 | |||||||||||||||
| 154 | grad uses prototypes to allow for a variety of usages. In its most basic form, | ||||||||||||||
| 155 | it accepts only one argument which may either be a Math::Symbolic tree or a | ||||||||||||||
| 156 | string both of which will be interpreted as the function to compute the | ||||||||||||||
| 157 | gradient for. Optionally, you may specify a second argument which must | ||||||||||||||
| 158 | be a (literal) array of Math::Symbolic::Variable objects or valid | ||||||||||||||
| 159 | Math::Symbolic variable names (strings). These variables will the be used for | ||||||||||||||
| 160 | the gradient instead of the x1, ..., xn inferred from the function signature. | ||||||||||||||
| 161 | |||||||||||||||
| 162 | =cut | ||||||||||||||
| 163 | |||||||||||||||
| 164 | 14 | ▶ | sub grad ($;\@) { | ||||||||||||
|
Subroutine
grad: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||
| 165 | 14 | my $original = shift; | |||||||||||||
| 166 | 14 | ▶ | $original = parse_from_string($original) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 167 | unless ref($original) =~ /^Math::Symbolic/; | ||||||||||||||
| 168 | 14 | my $signature = shift; | |||||||||||||
| 169 | |||||||||||||||
| 170 | 14 | my @funcs; | |||||||||||||
| 171 | 14 | ▶ | my @signature = | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 172 | ( defined $signature ? @$signature : $original->signature() ); | ||||||||||||||
| 173 | |||||||||||||||
| 174 | 14 | foreach (@signature) { | |||||||||||||
| 175 | 33 | my $var = Math::Symbolic::Variable->new($_); | |||||||||||||
| 176 | 33 | my $func = Math::Symbolic::Operator->new( | |||||||||||||
| 177 | { | ||||||||||||||
| 178 | type => U_P_DERIVATIVE, | ||||||||||||||
| 179 | operands => [ $original->new(), $var ], | ||||||||||||||
| 180 | } | ||||||||||||||
| 181 | ); | ||||||||||||||
| 182 | 33 | push @funcs, $func; | |||||||||||||
| 183 | } | ||||||||||||||
| 184 | 14 | return @funcs; | |||||||||||||
| 185 | } | ||||||||||||||
| 186 | |||||||||||||||
| 187 | =head2 div | ||||||||||||||
| 188 | |||||||||||||||
| 189 | This subroutine computes the divergence of a set of Math::Symbolic trees | ||||||||||||||
| 190 | representing a vectorial function. | ||||||||||||||
| 191 | |||||||||||||||
| 192 | The divergence of a vectorial function | ||||||||||||||
| 193 | F = (f1(x1, ..., xn), ..., fn(x1, ..., xn)) is defined like follows: | ||||||||||||||
| 194 | |||||||||||||||
| 195 | sum_from_i=1_to_n( dfi(x1, ..., xn) / dxi ) | ||||||||||||||
| 196 | |||||||||||||||
| 197 | That is, the sum of all partial derivatives of the i-th component function | ||||||||||||||
| 198 | to the i-th coordinate. See your favourite book on calculus for details. | ||||||||||||||
| 199 | Obviously, it is important to keep in mind that the number of function | ||||||||||||||
| 200 | components must be equal to the number of variables/coordinates. | ||||||||||||||
| 201 | |||||||||||||||
| 202 | Similar to grad, div uses prototypes to offer a comfortable interface. | ||||||||||||||
| 203 | First argument must be a (literal) array of strings and Math::Symbolic trees | ||||||||||||||
| 204 | which represent the vectorial function's components. If no second argument | ||||||||||||||
| 205 | is passed, the variables used for computing the divergence will be | ||||||||||||||
| 206 | inferred from the functions. That means the function signatures will be | ||||||||||||||
| 207 | joined to form a signature for the vectorial function. | ||||||||||||||
| 208 | |||||||||||||||
| 209 | If the optional second argument is specified, it has to be a (literal) | ||||||||||||||
| 210 | array of Math::Symbolic::Variable objects and valid variable names (strings). | ||||||||||||||
| 211 | These will then be interpreted as the list of variables for computing the | ||||||||||||||
| 212 | divergence. | ||||||||||||||
| 213 | |||||||||||||||
| 214 | =cut | ||||||||||||||
| 215 | |||||||||||||||
| 216 | 3 | ▶ | sub div (\@;\@) { | ||||||||||||
|
Subroutine
div: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||
| 217 | my @originals = | ||||||||||||||
| 218 | 9 | ▶ | map { ( ref($_) =~ /^Math::Symbolic/ ) ? $_ : parse_from_string($_) } | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 219 | 3 | @{ +shift }; | |||||||||||||
| 220 | |||||||||||||||
| 221 | 3 | my $signature = shift; | |||||||||||||
| 222 | 3 | ▶ | $signature = _combined_signature(@originals) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 223 | if not defined $signature; | ||||||||||||||
| 224 | |||||||||||||||
| 225 | 3 | ▶ | if ( @$signature != @originals ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 226 | 0 | die "Variable count does not function count for divergence."; | |||||||||||||
| 227 | } | ||||||||||||||
| 228 | |||||||||||||||
| 229 | 3 | my @signature = map { Math::Symbolic::Variable->new($_) } @$signature; | |||||||||||||
| 230 | |||||||||||||||
| 231 | 3 | my $div = Math::Symbolic::Operator->new( | |||||||||||||
| 232 | { | ||||||||||||||
| 233 | type => U_P_DERIVATIVE, | ||||||||||||||
| 234 | operands => [ shift(@originals)->new(), shift @signature ], | ||||||||||||||
| 235 | } | ||||||||||||||
| 236 | ); | ||||||||||||||
| 237 | |||||||||||||||
| 238 | 3 | foreach (@originals) { | |||||||||||||
| 239 | 6 | $div = Math::Symbolic::Operator->new( | |||||||||||||
| 240 | '+', $div, | ||||||||||||||
| 241 | Math::Symbolic::Operator->new( | ||||||||||||||
| 242 | { | ||||||||||||||
| 243 | type => U_P_DERIVATIVE, | ||||||||||||||
| 244 | operands => [ $_->new(), shift @signature ], | ||||||||||||||
| 245 | } | ||||||||||||||
| 246 | ) | ||||||||||||||
| 247 | ); | ||||||||||||||
| 248 | } | ||||||||||||||
| 249 | 3 | return $div; | |||||||||||||
| 250 | } | ||||||||||||||
| 251 | |||||||||||||||
| 252 | =head2 rot | ||||||||||||||
| 253 | |||||||||||||||
| 254 | This subroutine computes the rotation of a set of three Math::Symbolic trees | ||||||||||||||
| 255 | representing a vectorial function. | ||||||||||||||
| 256 | |||||||||||||||
| 257 | The rotation of a vectorial function | ||||||||||||||
| 258 | F = (f1(x1, x2, x3), f2(x1, x2, x3), f3(x1, x2, x3)) is defined as the | ||||||||||||||
| 259 | following vector: | ||||||||||||||
| 260 | |||||||||||||||
| 261 | ( ( df3/dx2 - df2/dx3 ), | ||||||||||||||
| 262 | ( df1/dx3 - df3/dx1 ), | ||||||||||||||
| 263 | ( df2/dx1 - df1/dx2 ) ) | ||||||||||||||
| 264 | |||||||||||||||
| 265 | Or "nabla x F" for short. Again, I have to refer to the literature for | ||||||||||||||
| 266 | the details on what rotation is. Please note that there have to be | ||||||||||||||
| 267 | exactly three function components and three coordinates because the cross | ||||||||||||||
| 268 | product and hence rotation is only defined in three dimensions. | ||||||||||||||
| 269 | |||||||||||||||
| 270 | As with the previously introduced subroutines div and grad, rot | ||||||||||||||
| 271 | offers a prototyped interface. | ||||||||||||||
| 272 | First argument must be a (literal) array of strings and Math::Symbolic trees | ||||||||||||||
| 273 | which represent the vectorial function's components. If no second argument | ||||||||||||||
| 274 | is passed, the variables used for computing the rotation will be | ||||||||||||||
| 275 | inferred from the functions. That means the function signatures will be | ||||||||||||||
| 276 | joined to form a signature for the vectorial function. | ||||||||||||||
| 277 | |||||||||||||||
| 278 | If the optional second argument is specified, it has to be a (literal) | ||||||||||||||
| 279 | array of Math::Symbolic::Variable objects and valid variable names (strings). | ||||||||||||||
| 280 | These will then be interpreted as the list of variables for computing the | ||||||||||||||
| 281 | rotation. (And please excuse my copying the last two paragraphs from above.) | ||||||||||||||
| 282 | |||||||||||||||
| 283 | =cut | ||||||||||||||
| 284 | |||||||||||||||
| 285 | 1 | ▶ | sub rot (\@;\@) { | ||||||||||||
|
Subroutine
rot: called CC=5 SCAR=17.8
Pod
documented
| |||||||||||||||
| 286 | 1 | my $originals = shift; | |||||||||||||
| 287 | my @originals = | ||||||||||||||
| 288 | 1 | ▶ | map { ( ref($_) =~ /^Math::Symbolic/ ) ? $_ : parse_from_string($_) } | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 289 | @$originals; | ||||||||||||||
| 290 | |||||||||||||||
| 291 | 1 | my $signature = shift; | |||||||||||||
| 292 | 1 | ▶ | $signature = _combined_signature(@originals) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 293 | unless defined $signature; | ||||||||||||||
| 294 | |||||||||||||||
| 295 | 1 | ▶ | if ( @originals != 3 ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 296 | 0 | die "Rotation only defined for functions of three components."; | |||||||||||||
| 297 | } | ||||||||||||||
| 298 | 1 | ▶ | if ( @$signature != 3 ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 299 | 0 | die "Rotation only defined for three variables."; | |||||||||||||
| 300 | } | ||||||||||||||
| 301 | |||||||||||||||
| 302 | return ( | ||||||||||||||
| 303 | 1 | Math::Symbolic::Operator->new( | |||||||||||||
| 304 | '-', | ||||||||||||||
| 305 | Math::Symbolic::Operator->new( | ||||||||||||||
| 306 | { | ||||||||||||||
| 307 | type => U_P_DERIVATIVE, | ||||||||||||||
| 308 | operands => [ $originals[2]->new(), $signature->[1] ], | ||||||||||||||
| 309 | } | ||||||||||||||
| 310 | ), | ||||||||||||||
| 311 | Math::Symbolic::Operator->new( | ||||||||||||||
| 312 | { | ||||||||||||||
| 313 | type => U_P_DERIVATIVE, | ||||||||||||||
| 314 | operands => [ $originals[1]->new(), $signature->[2] ], | ||||||||||||||
| 315 | } | ||||||||||||||
| 316 | ) | ||||||||||||||
| 317 | ), | ||||||||||||||
| 318 | Math::Symbolic::Operator->new( | ||||||||||||||
| 319 | '-', | ||||||||||||||
| 320 | Math::Symbolic::Operator->new( | ||||||||||||||
| 321 | { | ||||||||||||||
| 322 | type => U_P_DERIVATIVE, | ||||||||||||||
| 323 | operands => [ $originals[0]->new(), $signature->[2] ], | ||||||||||||||
| 324 | } | ||||||||||||||
| 325 | ), | ||||||||||||||
| 326 | Math::Symbolic::Operator->new( | ||||||||||||||
| 327 | { | ||||||||||||||
| 328 | type => U_P_DERIVATIVE, | ||||||||||||||
| 329 | operands => [ $originals[2]->new(), $signature->[0] ], | ||||||||||||||
| 330 | } | ||||||||||||||
| 331 | ) | ||||||||||||||
| 332 | ), | ||||||||||||||
| 333 | Math::Symbolic::Operator->new( | ||||||||||||||
| 334 | '-', | ||||||||||||||
| 335 | Math::Symbolic::Operator->new( | ||||||||||||||
| 336 | { | ||||||||||||||
| 337 | type => U_P_DERIVATIVE, | ||||||||||||||
| 338 | operands => [ $originals[1]->new(), $signature->[0] ], | ||||||||||||||
| 339 | } | ||||||||||||||
| 340 | ), | ||||||||||||||
| 341 | Math::Symbolic::Operator->new( | ||||||||||||||
| 342 | { | ||||||||||||||
| 343 | type => U_P_DERIVATIVE, | ||||||||||||||
| 344 | operands => [ $originals[0]->new(), $signature->[1] ], | ||||||||||||||
| 345 | } | ||||||||||||||
| 346 | ) | ||||||||||||||
| 347 | ) | ||||||||||||||
| 348 | ); | ||||||||||||||
| 349 | } | ||||||||||||||
| 350 | |||||||||||||||
| 351 | =head2 Jacobi | ||||||||||||||
| 352 | |||||||||||||||
| 353 | Jacobi() returns the Jacobi matrix of a given vectorial function. | ||||||||||||||
| 354 | It expects any number of arguments (strings and/or Math::Symbolic trees) | ||||||||||||||
| 355 | which will be interpreted as the vectorial function's components. | ||||||||||||||
| 356 | Variables used for computing the matrix are, by default, inferred from the | ||||||||||||||
| 357 | combined signature of the components. By specifying a second literal | ||||||||||||||
| 358 | array of variable names as (second) argument, you may override this | ||||||||||||||
| 359 | behaviour. | ||||||||||||||
| 360 | |||||||||||||||
| 361 | The Jacobi matrix is the vector of gradient vectors of the vectorial | ||||||||||||||
| 362 | function's components. | ||||||||||||||
| 363 | |||||||||||||||
| 364 | =cut | ||||||||||||||
| 365 | |||||||||||||||
| 366 | 2 | ▶ | sub Jacobi (\@;\@) { | ||||||||||||
|
Subroutine
Jacobi: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||
| 367 | my @funcs = | ||||||||||||||
| 368 | 5 | ▶ | map { ( ref($_) =~ /^Math::Symbolic/ ) ? $_ : parse_from_string($_) } | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 369 | 2 | @{ +shift() }; | |||||||||||||
| 370 | |||||||||||||||
| 371 | 2 | my $signature = shift; | |||||||||||||
| 372 | my @signature = ( | ||||||||||||||
| 373 | defined $signature | ||||||||||||||
| 374 | ? ( | ||||||||||||||
| 375 | map { | ||||||||||||||
| 376 | 2 | ▶ | ( ref($_) =~ /^Math::Symbolic/ ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 377 | ? $_ | ||||||||||||||
| 378 | : parse_from_string($_) | ||||||||||||||
| 379 | } @$signature | ||||||||||||||
| 380 | ) | ||||||||||||||
| 381 | 2 | ▶ | : ( @{ +_combined_signature(@funcs) } ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 382 | ); | ||||||||||||||
| 383 | |||||||||||||||
| 384 | 2 | return map { [ grad $_, @signature ] } @funcs; | |||||||||||||
| 385 | } | ||||||||||||||
| 386 | |||||||||||||||
| 387 | =head2 Hesse | ||||||||||||||
| 388 | |||||||||||||||
| 389 | Hesse() returns the Hesse matrix of a given scalar function. First | ||||||||||||||
| 390 | argument must be a string (to be parsed as a Math::Symbolic tree) | ||||||||||||||
| 391 | or a Math::Symbolic tree. As with Jacobi(), Hesse() optionally | ||||||||||||||
| 392 | accepts an array of signature variables as second argument. | ||||||||||||||
| 393 | |||||||||||||||
| 394 | The Hesse matrix is the Jacobi matrix of the gradient of a scalar function. | ||||||||||||||
| 395 | |||||||||||||||
| 396 | =cut | ||||||||||||||
| 397 | |||||||||||||||
| 398 | 1 | ▶ | sub Hesse ($;\@) { | ||||||||||||
|
Subroutine
Hesse: called CC=4 SCAR=15.9
Pod
documented
| |||||||||||||||
| 399 | 1 | my $function = shift; | |||||||||||||
| 400 | 1 | ▶ | $function = parse_from_string($function) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 401 | unless ref($function) =~ /^Math::Symbolic/; | ||||||||||||||
| 402 | 1 | my $signature = shift; | |||||||||||||
| 403 | my @signature = ( | ||||||||||||||
| 404 | defined $signature | ||||||||||||||
| 405 | ? ( | ||||||||||||||
| 406 | map { | ||||||||||||||
| 407 | 0 | ▶ | ( ref($_) =~ /^Math::Symbolic/ ) | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 408 | ? $_ | ||||||||||||||
| 409 | : parse_from_string($_) | ||||||||||||||
| 410 | } @$signature | ||||||||||||||
| 411 | ) | ||||||||||||||
| 412 | : $function->signature() | ||||||||||||||
| 413 | ); | ||||||||||||||
| 414 | |||||||||||||||
| 415 | 1 | my @gradient = grad $function, @signature; | |||||||||||||
| 416 | 1 | return Jacobi @gradient, @signature; | |||||||||||||
| 417 | } | ||||||||||||||
| 418 | |||||||||||||||
| 419 | =head2 TotalDifferential | ||||||||||||||
| 420 | |||||||||||||||
| 421 | This function computes the total differential of a scalar function of | ||||||||||||||
| 422 | multiple variables in a certain point. | ||||||||||||||
| 423 | |||||||||||||||
| 424 | First argument must be the function to derive. The second argument is | ||||||||||||||
| 425 | an optional (literal) array of variable names (strings) and | ||||||||||||||
| 426 | Math::Symbolic::Variable objects to be used for deriving. If the argument | ||||||||||||||
| 427 | is not specified, the functions signature will be used. The third argument | ||||||||||||||
| 428 | is also an optional array and denotes the set of variable (names) to use for | ||||||||||||||
| 429 | indicating the point for which to evaluate the differential. It must have | ||||||||||||||
| 430 | the same number of elements as the second argument. | ||||||||||||||
| 431 | If not specified the variable names used as coordinated (the second argument) | ||||||||||||||
| 432 | with an appended '_0' will be used as the point's components. | ||||||||||||||
| 433 | |||||||||||||||
| 434 | =cut | ||||||||||||||
| 435 | |||||||||||||||
| 436 | 3 | ▶ | sub TotalDifferential ($;\@\@) { | ||||||||||||
|
Subroutine
TotalDifferential: called CC=8 SCAR=21.0
Pod
documented
| |||||||||||||||
| 437 | 3 | my $function = shift; | |||||||||||||
| 438 | 3 | ▶ | $function = parse_from_string($function) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 439 | unless ref($function) =~ /^Math::Symbolic/; | ||||||||||||||
| 440 | |||||||||||||||
| 441 | 3 | my $sig = shift; | |||||||||||||
| 442 | 3 | ▶ | $sig = [ $function->signature() ] if not defined $sig; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 443 | 3 | my @sig = map { Math::Symbolic::Variable->new($_) } @$sig; | |||||||||||||
| 444 | |||||||||||||||
| 445 | 3 | my $point = shift; | |||||||||||||
| 446 | 3 | ▶ | $point = [ map { $_->name() . '_0' } @sig ] if not defined $point; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 447 | 3 | my @point = map { Math::Symbolic::Variable->new($_) } @$point; | |||||||||||||
| 448 | |||||||||||||||
| 449 | 3 | ▶ | if ( @point != @sig ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 450 | 0 | croak "Signature dimension does not match point dimension."; | |||||||||||||
| 451 | } | ||||||||||||||
| 452 | |||||||||||||||
| 453 | 3 | my @grad = grad $function, @sig; | |||||||||||||
| 454 | 3 | ▶ | if ( @grad != @sig ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 455 | 0 | croak "Signature dimension does not match function grad dim."; | |||||||||||||
| 456 | } | ||||||||||||||
| 457 | |||||||||||||||
| 458 | 3 | foreach (@grad) { | |||||||||||||
| 459 | 6 | my @point_copy = @point; | |||||||||||||
| 460 | 6 | $_->implement( map { ( $_->name() => shift(@point_copy) ) } @sig ); | |||||||||||||
| 461 | } | ||||||||||||||
| 462 | |||||||||||||||
| 463 | 3 | my $d = | |||||||||||||
| 464 | Math::Symbolic::Operator->new( '*', shift(@grad), | ||||||||||||||
| 465 | Math::Symbolic::Operator->new( '-', shift(@sig), shift(@point) ) ); | ||||||||||||||
| 466 | |||||||||||||||
| 467 | 3 | ▶ | $d += | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 468 | Math::Symbolic::Operator->new( '*', shift(@grad), | ||||||||||||||
| 469 | Math::Symbolic::Operator->new( '-', shift(@sig), shift(@point) ) ) | ||||||||||||||
| 470 | while @grad; | ||||||||||||||
| 471 | |||||||||||||||
| 472 | 3 | return $d; | |||||||||||||
| 473 | } | ||||||||||||||
| 474 | |||||||||||||||
| 475 | =head2 DirectionalDerivative | ||||||||||||||
| 476 | |||||||||||||||
| 477 | DirectionalDerivative computes the directional derivative of a scalar function | ||||||||||||||
| 478 | in the direction of a specified vector. With f being the function and X, A being | ||||||||||||||
| 479 | vectors, it looks like this: (this is a partial derivative) | ||||||||||||||
| 480 | |||||||||||||||
| 481 | df(X)/dA = grad(f(X)) * (A / |A|) | ||||||||||||||
| 482 | |||||||||||||||
| 483 | First argument must be the function to derive (either a string or a valid | ||||||||||||||
| 484 | Math::Symbolic tree). Second argument must be vector into whose direction to | ||||||||||||||
| 485 | derive. It is to be specified as an array of variable names and objects. | ||||||||||||||
| 486 | Third argument is the optional signature to be used for computing the gradient. | ||||||||||||||
| 487 | Please see the documentation of the grad function for details. It's | ||||||||||||||
| 488 | dimension must match that of the directional vector. | ||||||||||||||
| 489 | |||||||||||||||
| 490 | =cut | ||||||||||||||
| 491 | |||||||||||||||
| 492 | 2 | ▶ | sub DirectionalDerivative ($\@;\@) { | ||||||||||||
|
Subroutine
DirectionalDerivative: called CC=7 SCAR=19.6
Pod
documented
| |||||||||||||||
| 493 | 2 | my $function = shift; | |||||||||||||
| 494 | 2 | ▶ | $function = parse_from_string($function) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 495 | unless ref($function) =~ /^Math::Symbolic/; | ||||||||||||||
| 496 | |||||||||||||||
| 497 | 2 | my $vec = shift; | |||||||||||||
| 498 | 2 | my @vec = map { Math::Symbolic::Variable->new($_) } @$vec; | |||||||||||||
| 499 | |||||||||||||||
| 500 | 2 | my $sig = shift; | |||||||||||||
| 501 | 2 | ▶ | $sig = [ $function->signature() ] if not defined $sig; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 502 | 2 | my @sig = map { Math::Symbolic::Variable->new($_) } @$sig; | |||||||||||||
| 503 | |||||||||||||||
| 504 | 2 | ▶ | if ( @vec != @sig ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 505 | 0 | croak "Signature dimension does not match vector dimension."; | |||||||||||||
| 506 | } | ||||||||||||||
| 507 | |||||||||||||||
| 508 | 2 | my @grad = grad $function, @sig; | |||||||||||||
| 509 | 2 | ▶ | if ( @grad != @sig ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 510 | 0 | croak "Signature dimension does not match function grad dim."; | |||||||||||||
| 511 | } | ||||||||||||||
| 512 | |||||||||||||||
| 513 | 2 | my $two = Math::Symbolic::Constant->new(2); | |||||||||||||
| 514 | my @squares = | ||||||||||||||
| 515 | 2 | map { Math::Symbolic::Operator->new( '^', $_, $two ) } @vec; | |||||||||||||
| 516 | |||||||||||||||
| 517 | 2 | my $abs_vec = shift @squares; | |||||||||||||
| 518 | 2 | ▶ | $abs_vec += shift(@squares) while @squares; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 519 | |||||||||||||||
| 520 | 2 | $abs_vec = | |||||||||||||
| 521 | Math::Symbolic::Operator->new( '^', $abs_vec, | ||||||||||||||
| 522 | Math::Symbolic::Constant->new( 1 / 2 ) ); | ||||||||||||||
| 523 | |||||||||||||||
| 524 | 2 | @vec = map { $_ / $abs_vec } @vec; | |||||||||||||
| 525 | |||||||||||||||
| 526 | 2 | my $dd = Math::Symbolic::Operator->new( '*', shift(@grad), shift(@vec) ); | |||||||||||||
| 527 | |||||||||||||||
| 528 | 2 | ▶ | $dd += Math::Symbolic::Operator->new( '*', shift(@grad), shift(@vec) ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 529 | while @grad; | ||||||||||||||
| 530 | |||||||||||||||
| 531 | 2 | return $dd; | |||||||||||||
| 532 | } | ||||||||||||||
| 533 | |||||||||||||||
| 534 | =begin comment | ||||||||||||||
| 535 | |||||||||||||||
| 536 | This computes the taylor binomial | ||||||||||||||
| 537 | |||||||||||||||
| 538 | (d/dx*(x-x0)+d/dy*(y-y0))^n * f(x0, y0) | ||||||||||||||
| 539 | |||||||||||||||
| 540 | =end comment | ||||||||||||||
| 541 | |||||||||||||||
| 542 | =cut | ||||||||||||||
| 543 | |||||||||||||||
| 544 | 1 | ▶ | sub _taylor_binomial { | ||||||||||||
|
Subroutine
_taylor_binomial: called CC=7 SCAR=26.7
| |||||||||||||||
| 545 | 1 | my $f = shift; | |||||||||||||
| 546 | 1 | my $a = shift; | |||||||||||||
| 547 | 1 | my $b = shift; | |||||||||||||
| 548 | 1 | my $a0 = shift; | |||||||||||||
| 549 | 1 | my $b0 = shift; | |||||||||||||
| 550 | 1 | my $n = shift; | |||||||||||||
| 551 | |||||||||||||||
| 552 | 1 | $f = $f->new(); | |||||||||||||
| 553 | 1 | my $da = $a - $a0; | |||||||||||||
| 554 | 1 | my $db = $b - $b0; | |||||||||||||
| 555 | |||||||||||||||
| 556 | 1 | $f->implement( $a->name() => $a0, $b->name() => $b0 ); | |||||||||||||
| 557 | |||||||||||||||
| 558 | 1 | ▶ | return Math::Symbolic::Constant->one() if $n == 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 559 | 1 | ▶ | return $da * | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 560 | Math::Symbolic::Operator->new( 'partial_derivative', $f->new(), $a0 ) + | ||||||||||||||
| 561 | $db * | ||||||||||||||
| 562 | Math::Symbolic::Operator->new( 'partial_derivative', $f->new(), $b0 ) | ||||||||||||||
| 563 | if $n == 1; | ||||||||||||||
| 564 | |||||||||||||||
| 565 | 0 | my $n_obj = Math::Symbolic::Constant->new($n); | |||||||||||||
| 566 | |||||||||||||||
| 567 | 0 | my $p_a_deriv = $f->new(); | |||||||||||||
| 568 | $p_a_deriv = | ||||||||||||||
| 569 | Math::Symbolic::Operator->new( 'partial_derivative', $p_a_deriv, $a0 ) | ||||||||||||||
| 570 | 0 | for 1 .. $n; | |||||||||||||
| 571 | |||||||||||||||
| 572 | 0 | my $res = | |||||||||||||
| 573 | Math::Symbolic::Operator->new( '*', $p_a_deriv, | ||||||||||||||
| 574 | Math::Symbolic::Operator->new( '^', $da, $n_obj ) ); | ||||||||||||||
| 575 | |||||||||||||||
| 576 | 0 | foreach my $k ( 1 .. $n - 1 ) { | |||||||||||||
| 577 | 0 | $p_a_deriv = $p_a_deriv->op1()->new(); | |||||||||||||
| 578 | |||||||||||||||
| 579 | 0 | my $deriv = $p_a_deriv; | |||||||||||||
| 580 | $deriv = | ||||||||||||||
| 581 | Math::Symbolic::Operator->new( 'partial_derivative', $deriv, $b0 ) | ||||||||||||||
| 582 | 0 | for 1 .. $k; | |||||||||||||
| 583 | |||||||||||||||
| 584 | 0 | my $k_obj = Math::Symbolic::Constant->new($k); | |||||||||||||
| 585 | 0 | $res += Math::Symbolic::Operator->new( | |||||||||||||
| 586 | '*', | ||||||||||||||
| 587 | Math::Symbolic::Constant->new( _over( $n, $k ) ), | ||||||||||||||
| 588 | Math::Symbolic::Operator->new( | ||||||||||||||
| 589 | '*', $deriv, | ||||||||||||||
| 590 | Math::Symbolic::Operator->new( | ||||||||||||||
| 591 | '*', | ||||||||||||||
| 592 | Math::Symbolic::Operator->new( | ||||||||||||||
| 593 | '^', $da, Math::Symbolic::Constant->new( $n - $k ) | ||||||||||||||
| 594 | ), | ||||||||||||||
| 595 | Math::Symbolic::Operator->new( '^', $db, $k_obj ) | ||||||||||||||
| 596 | ) | ||||||||||||||
| 597 | ) | ||||||||||||||
| 598 | ); | ||||||||||||||
| 599 | } | ||||||||||||||
| 600 | |||||||||||||||
| 601 | 0 | my $p_b_deriv = $f->new(); | |||||||||||||
| 602 | $p_b_deriv = | ||||||||||||||
| 603 | Math::Symbolic::Operator->new( 'partial_derivative', $p_b_deriv, $b0 ) | ||||||||||||||
| 604 | 0 | for 1 .. $n; | |||||||||||||
| 605 | |||||||||||||||
| 606 | 0 | $res += | |||||||||||||
| 607 | Math::Symbolic::Operator->new( '*', $p_b_deriv, | ||||||||||||||
| 608 | Math::Symbolic::Operator->new( '^', $db, $n_obj ) ); | ||||||||||||||
| 609 | |||||||||||||||
| 610 | 0 | return $res; | |||||||||||||
| 611 | } | ||||||||||||||
| 612 | |||||||||||||||
| 613 | =begin comment | ||||||||||||||
| 614 | |||||||||||||||
| 615 | This computes | ||||||||||||||
| 616 | |||||||||||||||
| 617 | / n \ | ||||||||||||||
| 618 | | | | ||||||||||||||
| 619 | \ k / | ||||||||||||||
| 620 | |||||||||||||||
| 621 | =end comment | ||||||||||||||
| 622 | |||||||||||||||
| 623 | =cut | ||||||||||||||
| 624 | |||||||||||||||
| 625 | 0 | ▶ | sub _over { | ||||||||||||
|
Subroutine
_over: not called CC=5 SCAR=34.0
| |||||||||||||||
| 626 | 0 | my $n = shift; | |||||||||||||
| 627 | 0 | my $k = shift; | |||||||||||||
| 628 | |||||||||||||||
| 629 | 0 | ▶ | return 1 if $k == 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 630 | 0 | ▶ | return _over( $n, $n - $k ) if $k > $n / 2; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 631 | |||||||||||||||
| 632 | 0 | my $prod = 1; | |||||||||||||
| 633 | 0 | my $i = $n; | |||||||||||||
| 634 | 0 | my $j = $k; | |||||||||||||
| 635 | 0 | ▶ | while ( $i > $k ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 636 | 0 | $prod *= $i; | |||||||||||||
| 637 | 0 | ▶ | $prod /= $j if $j > 1; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 638 | 0 | $i--; | |||||||||||||
| 639 | 0 | $j--; | |||||||||||||
| 640 | } | ||||||||||||||
| 641 | |||||||||||||||
| 642 | 0 | return ($prod); | |||||||||||||
| 643 | } | ||||||||||||||
| 644 | |||||||||||||||
| 645 | =begin comment | ||||||||||||||
| 646 | |||||||||||||||
| 647 | _faculty() computes the product that is the faculty of the | ||||||||||||||
| 648 | first argument. | ||||||||||||||
| 649 | |||||||||||||||
| 650 | =end comment | ||||||||||||||
| 651 | |||||||||||||||
| 652 | =cut | ||||||||||||||
| 653 | |||||||||||||||
| 654 | 1 | ▶ | sub _faculty { | ||||||||||||
|
Subroutine
_faculty: called CC=4 SCAR=18.7
| |||||||||||||||
| 655 | 1 | my $num = shift; | |||||||||||||
| 656 | 1 | ▶ | croak "Cannot calculate faculty of negative numbers." | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 657 | if $num < 0; | ||||||||||||||
| 658 | 1 | my $fac = Math::Symbolic::Constant->one(); | |||||||||||||
| 659 | 1 | ▶ | return $fac if $num <= 1; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 660 | 0 | ▶ | for ( my $i = 2 ; $i <= $num ; $i++ ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 661 | 0 | $fac *= Math::Symbolic::Constant->new($i); | |||||||||||||
| 662 | } | ||||||||||||||
| 663 | 0 | return $fac; | |||||||||||||
| 664 | } | ||||||||||||||
| 665 | |||||||||||||||
| 666 | =head2 TaylorPolyTwoDim | ||||||||||||||
| 667 | |||||||||||||||
| 668 | This subroutine computes the Taylor Polynomial for functions of two | ||||||||||||||
| 669 | variables. Please refer to the documentation of the TaylorPolynomial | ||||||||||||||
| 670 | function in the Math::Symbolic::MiscCalculus package for an explanation | ||||||||||||||
| 671 | of single dimensional Taylor Polynomials. This is the counterpart in | ||||||||||||||
| 672 | two dimensions. | ||||||||||||||
| 673 | |||||||||||||||
| 674 | First argument must be the function to approximate with the Taylor Polynomial | ||||||||||||||
| 675 | either as a string or a Math::Symbolic tree. Second and third argument | ||||||||||||||
| 676 | must be the names of the two coordinates. (These may alternatively be | ||||||||||||||
| 677 | Math::Symbolic::Variable objects.) Fourth argument must be | ||||||||||||||
| 678 | the degree of the Taylor Polynomial. Fifth and Sixth arguments are optional | ||||||||||||||
| 679 | and specify the names of the variables to introduce as the point of | ||||||||||||||
| 680 | approximation. These default to the names of the coordinates with '_0' | ||||||||||||||
| 681 | appended. | ||||||||||||||
| 682 | |||||||||||||||
| 683 | =cut | ||||||||||||||
| 684 | |||||||||||||||
| 685 | 2 | ▶ | sub TaylorPolyTwoDim ($$$$;$$) { | ||||||||||||
|
Subroutine
TaylorPolyTwoDim: called CC=10 SCAR=23.5
Pod
documented
| |||||||||||||||
| 686 | 2 | my $function = shift; | |||||||||||||
| 687 | 2 | ▶ | $function = parse_from_string($function) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 688 | unless ref($function) =~ /^Math::Symbolic/; | ||||||||||||||
| 689 | |||||||||||||||
| 690 | 2 | my $x1 = shift; | |||||||||||||
| 691 | 2 | ▶ | $x1 = Math::Symbolic::Variable->new($x1) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 692 | unless ref($x1) eq 'Math::Symbolic::Variable'; | ||||||||||||||
| 693 | 2 | my $x2 = shift; | |||||||||||||
| 694 | 2 | ▶ | $x2 = Math::Symbolic::Variable->new($x2) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 695 | unless ref($x2) eq 'Math::Symbolic::Variable'; | ||||||||||||||
| 696 | |||||||||||||||
| 697 | 2 | my $n = shift; | |||||||||||||
| 698 | |||||||||||||||
| 699 | 2 | my $x1_0 = shift; | |||||||||||||
| 700 | 2 | ▶ | $x1_0 = $x1->name() . '_0' if not defined $x1_0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 701 | 2 | ▶ | $x1_0 = Math::Symbolic::Variable->new($x1_0) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 702 | unless ref($x1_0) eq 'Math::Symbolic::Variable'; | ||||||||||||||
| 703 | |||||||||||||||
| 704 | 2 | my $x2_0 = shift; | |||||||||||||
| 705 | 2 | ▶ | $x2_0 = $x2->name() . '_0' if not defined $x2_0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 706 | 2 | ▶ | $x2_0 = Math::Symbolic::Variable->new($x2_0) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 707 | unless ref($x2_0) eq 'Math::Symbolic::Variable'; | ||||||||||||||
| 708 | |||||||||||||||
| 709 | 2 | my $x1_n = $x1->name(); | |||||||||||||
| 710 | 2 | my $x2_n = $x2->name(); | |||||||||||||
| 711 | |||||||||||||||
| 712 | 2 | my $dx1 = $x1 - $x1_0; | |||||||||||||
| 713 | 2 | my $dx2 = $x2 - $x2_0; | |||||||||||||
| 714 | |||||||||||||||
| 715 | 2 | my $copy = $function->new(); | |||||||||||||
| 716 | 2 | $copy->implement( $x1_n => $x1_0, $x2_n => $x2_0 ); | |||||||||||||
| 717 | |||||||||||||||
| 718 | 2 | my $taylor = $copy; | |||||||||||||
| 719 | |||||||||||||||
| 720 | 2 | ▶ | return $taylor if $n == 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 721 | |||||||||||||||
| 722 | 1 | foreach my $k ( 1 .. $n ) { | |||||||||||||
| 723 | 1 | $taylor += | |||||||||||||
| 724 | Math::Symbolic::Operator->new( '/', | ||||||||||||||
| 725 | _taylor_binomial( $function->new(), $x1, $x2, $x1_0, $x2_0, $k ), | ||||||||||||||
| 726 | _faculty($k) ); | ||||||||||||||
| 727 | } | ||||||||||||||
| 728 | |||||||||||||||
| 729 | 1 | return $taylor; | |||||||||||||
| 730 | } | ||||||||||||||
| 731 | |||||||||||||||
| 732 | =head2 WronskyDet | ||||||||||||||
| 733 | |||||||||||||||
| 734 | WronskyDet() computes the Wronsky Determinant of a set of n functions. | ||||||||||||||
| 735 | |||||||||||||||
| 736 | First argument is required and a (literal) array of n functions. Second | ||||||||||||||
| 737 | argument is optional and a (literal) array of n variables or variable names. | ||||||||||||||
| 738 | If the second argument is omitted, the variables used for deriving are inferred | ||||||||||||||
| 739 | from function signatures. This requires, however, that the function signatures | ||||||||||||||
| 740 | have exactly one element. (And the function this exactly one variable.) | ||||||||||||||
| 741 | |||||||||||||||
| 742 | =cut | ||||||||||||||
| 743 | |||||||||||||||
| 744 | 1 | ▶ | sub WronskyDet (\@;\@) { | ||||||||||||
|
Subroutine
WronskyDet: called CC=7 SCAR=21.2
Pod
documented
| |||||||||||||||
| 745 | 1 | my $functions = shift; | |||||||||||||
| 746 | my @functions = | ||||||||||||||
| 747 | 1 | ▶ | map { ( ref($_) =~ /^Math::Symbolic/ ) ? $_ : parse_from_string($_) } | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 748 | @$functions; | ||||||||||||||
| 749 | 1 | my $vars = shift; | |||||||||||||
| 750 | 1 | ▶ | my @vars = ( defined $vars ? @$vars : () ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 751 | @vars = map { | ||||||||||||||
| 752 | 0 | ▶ | my @sig = $_->signature(); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 753 | 0 | ▶ | croak "Cannot infer function signature for WronskyDet." | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 754 | if @sig != 1; | ||||||||||||||
| 755 | 0 | shift @sig; | |||||||||||||
| 756 | } @functions if not defined $vars; | ||||||||||||||
| 757 | 1 | @vars = map { Math::Symbolic::Variable->new($_) } @vars; | |||||||||||||
| 758 | 1 | ▶ | croak "Number of vars doesn't match num of functions in WronskyDet." | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 759 | if not @vars == @functions; | ||||||||||||||
| 760 | |||||||||||||||
| 761 | 1 | my @matrix; | |||||||||||||
| 762 | 1 | push @matrix, [@functions]; | |||||||||||||
| 763 | 1 | foreach ( 2 .. @functions ) { | |||||||||||||
| 764 | 1 | my $i = 0; | |||||||||||||
| 765 | @functions = map { | ||||||||||||||
| 766 | 1 | Math::Symbolic::Operator->new( 'partial_derivative', $_, | |||||||||||||
| 767 | $vars[ $i++ ] ) | ||||||||||||||
| 768 | } @functions; | ||||||||||||||
| 769 | 1 | push @matrix, [@functions]; | |||||||||||||
| 770 | } | ||||||||||||||
| 771 | 1 | return det @matrix; | |||||||||||||
| 772 | } | ||||||||||||||
| 773 | |||||||||||||||
| 774 | 2 | 1; | |||||||||||||
| 775 | __END__ | ||||||||||||||