| 1 | # | ||
| 2 | # GENERATED WITH PDL::PP from func.pd! Don't modify! | ||
| 3 | # | ||
| 4 | package PDL::Fit::Levmar::Func; | ||
| 5 | |||
| 6 | 11 | our @EXPORT_OK = qw(levmar_func _callf _callj _callj1 ); | |
| 7 | 11 | our %EXPORT_TAGS = (Func=>\@EXPORT_OK); | |
| 8 | |||
| 9 | 13 | ▶ | use PDL::Core; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 13 | ▶ | use PDL::Exporter; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 11 | ▶ | use DynaLoader; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | |||
| 13 | |||
| 14 | 11 | our $VERSION = '0.0103'; | |
| 15 | 11 | our @ISA = ( 'PDL::Exporter','DynaLoader' ); | |
| 16 | 11 | push @PDL::Core::PP, __PACKAGE__; | |
| 17 | 11 | bootstrap PDL::Fit::Levmar::Func $VERSION; | |
| 18 | |||
| 19 | |||
| 20 | |||
| 21 | |||
| 22 | |||
| 23 | |||
| 24 | |||
| 25 | |||
| 26 | #line 9 "func.pd" | ||
| 27 | |||
| 28 | =head1 NAME | ||
| 29 | |||
| 30 | PDL::Fit::Levmar::Func - Create model functions for Levenberg-Marquardt fit routines | ||
| 31 | |||
| 32 | =head1 DESCRIPTION | ||
| 33 | |||
| 34 | This module creates and manages functions for use with the | ||
| 35 | Levmar fitting module. The functions are created with a | ||
| 36 | very simple description language (that is mostly C), or are | ||
| 37 | pure C, or are perl code. For many applications, the present | ||
| 38 | document is not necessary because levmar uses the | ||
| 39 | Levmar::Func module transparantly. Therefore, before | ||
| 40 | reading the following, refer to L<PDL::Fit::Levmar>. | ||
| 41 | |||
| 42 | =head1 SYNOPSIS | ||
| 43 | |||
| 44 | use PDL::Fit::Levmar::Func; | ||
| 45 | |||
| 46 | $func = levmar_func( FUNC => # define and link function | ||
| 47 | |||
| 48 | ' | ||
| 49 | function gaussian | ||
| 50 | loop | ||
| 51 | x[i] = p[0] * exp(-(t[i] - p[1])*(t[i] - p[1])*p[2]); | ||
| 52 | end function | ||
| 53 | |||
| 54 | jacobian jacgaussian | ||
| 55 | double arg, expf; | ||
| 56 | loop | ||
| 57 | arg = t[i] - p[1]; | ||
| 58 | expf = exp(-arg*arg*p[2]); | ||
| 59 | d0 = expf; | ||
| 60 | d1 = p[0]*2*arg*p[2]*expf; | ||
| 61 | d2 = p[0]*(-arg*arg)*expf; | ||
| 62 | end jacobian | ||
| 63 | ' | ||
| 64 | ); | ||
| 65 | |||
| 66 | $hout = PDL::Fit::Levmar::levmar($p,$x,$t, # fit the data | ||
| 67 | FUNCTION => $func, | ||
| 68 | ); | ||
| 69 | |||
| 70 | =head1 FUNCTIONS | ||
| 71 | |||
| 72 | =head2 levmar_func() | ||
| 73 | |||
| 74 | =for usage | ||
| 75 | |||
| 76 | levmar_func( OPTIONS ) | ||
| 77 | |||
| 78 | =for ref | ||
| 79 | |||
| 80 | This function creates and links a function, ie, takes a function definition | ||
| 81 | and returns a function (object instance) ready for use by levmar. | ||
| 82 | see PDL::Fit::Levmar::Func for more information. | ||
| 83 | |||
| 84 | OPTIONS: | ||
| 85 | |||
| 86 | =for options | ||
| 87 | |||
| 88 | Some of the options are described in the PDL::Fit::Levmar documentation. | ||
| 89 | |||
| 90 | MKOBJ -- command to compile source into object code. This can be set. | ||
| 91 | The default value is determined by the perl installation and can | ||
| 92 | be determined by examining the Levmar::Func object returned by | ||
| 93 | new or the output hash of a call to levmar. A typical value is | ||
| 94 | cc -c -O2 -fPIC -o %o %c | ||
| 95 | |||
| 96 | MKSO -- command to convert object code to dynamic library code. A typical | ||
| 97 | default value is | ||
| 98 | cc -shared -L/usr/local/lib -fstack-protector %o -o %s | ||
| 99 | |||
| 100 | |||
| 101 | CTOP -- The value of this string will be written at the top of the c code | ||
| 102 | written by Levmar::Func. This can be used to include headers and so forth. | ||
| 103 | This option is actually set in the Levmar::Func object. | ||
| 104 | |||
| 105 | =head2 call() | ||
| 106 | |||
| 107 | =for ref | ||
| 108 | |||
| 109 | Call (evaluate) the fit function in a Levmar::Func object. | ||
| 110 | |||
| 111 | =for usage | ||
| 112 | |||
| 113 | use PDL::Fit::Levmar::Func; | ||
| 114 | |||
| 115 | $Gh = levmar_func(FUNC=>$fcode); | ||
| 116 | |||
| 117 | $x = $Gh->call($p,$t); | ||
| 118 | |||
| 119 | Here $fcode is a function definition (say lpp code). (This does | ||
| 120 | not currently work with perl subroutines, I think. Of course, | ||
| 121 | you can call the subroutine directly. But it would be good | ||
| 122 | to support it for consistency.) | ||
| 123 | |||
| 124 | $p is a pdl or ref to array of parameters. | ||
| 125 | $t is a pdl of co-ordinate values. | ||
| 126 | $x is the fit function evaluated at $p and $t. $x has the | ||
| 127 | same shape as $t. | ||
| 128 | |||
| 129 | =for example | ||
| 130 | |||
| 131 | $Gf = levmar_func( FUNC => ' | ||
| 132 | function | ||
| 133 | x = p0 * t * t; | ||
| 134 | '); | ||
| 135 | |||
| 136 | print $Gf->call([2],sequence(10)) , "\n"; | ||
| 137 | [0 2 8 18 32 50 72 98 128 162] | ||
| 138 | |||
| 139 | =cut | ||
| 140 | |||
| 141 | use strict; | ||
| 142 | use Carp; | ||
| 143 | use Config; | ||
| 144 | use File::Path; | ||
| 145 | use File::Spec::Functions; | ||
| 146 | |||
| 147 | sub munlink; | ||
| 148 | |||
| 149 | #use PDL::NiceSlice; | ||
| 150 | use PDL::Core ':Internal'; # For topdl() | ||
| 151 | |||
| 152 | use vars ('$MKOBJ', '$MKSO', '$OBJ_EXT' , '$SO', | ||
| 153 | '$LEVFUNC', '$JLEVFUNC', '$LPPEXT' ); | ||
| 154 | |||
| 155 | # These are pointers to the C functions that wrap the user's | ||
| 156 | # perl fit functions. | ||
| 157 | #$LEVFUNC = get_perl_func_wrap(); | ||
| 158 | #$JLEVFUNC = get_perl_jac_wrap(); | ||
| 159 | |||
| 160 | $OBJ_EXT = $Config{obj_ext}; | ||
| 161 | $SO = $Config{so}; | ||
| 162 | $LPPEXT = '.lpp'; | ||
| 163 | # string to compile .c to .o | ||
| 164 | $MKOBJ = $Config{cc} . " -c " . $Config{optimize} . " " . | ||
| 165 | $Config{cccdlflags} . " -o %o %c "; | ||
| 166 | |||
| 167 | # string to compile .o to .so | ||
| 168 | $MKSO = $Config{ld} . " " . $Config{lddlflags} ." %o -o %s "; | ||
| 169 | |||
| 170 | use File::Temp qw(tempdir); | ||
| 171 | |||
| 172 | #line 181 "func.pd" | ||
| 173 | |||
| 174 | =head2 new | ||
| 175 | |||
| 176 | =for usage | ||
| 177 | |||
| 178 | my $fitfunc = new PDL::Fit::Levmar::Func(...); | ||
| 179 | or | ||
| 180 | my $fitfunc = levmar_func(...); | ||
| 181 | |||
| 182 | =for ref | ||
| 183 | |||
| 184 | Generic constructor. Use levmar_func rather than new. | ||
| 185 | |||
| 186 | =cut | ||
| 187 | |||
| 188 | $PDL::Fit::Levmar::Func::SRC_EXT = ".c"; | ||
| 189 | #$PDL::Fit::Levmar::Func::SO_EXT = ".so"; | ||
| 190 | #$PDL::Fit::Levmar::Func::OBJ_EXT = ".o"; | ||
| 191 | |||
| 192 | sub deb { | ||
| 193 | # print STDERR $_[0], "\n"; | ||
| 194 | } | ||
| 195 | |||
| 196 | sub prwarn { print STDERR $_[0], "\n";} | ||
| 197 | |||
| 198 | ########################################## | ||
| 199 | # Methods follow | ||
| 200 | |||
| 201 | # some kind of stack corruption is related to this function, maybe. no probably not. | ||
| 202 | # So, only shift opts if they are there | ||
| 203 | |||
| 204 | sub new { | ||
| 205 | my($class) = shift; | ||
| 206 | my($opts) = shift; | ||
| 207 | # print STDERR "In new class: $class, opts $opts\n"; | ||
| 208 | if(ref $opts ne 'HASH') { # change opts to hash of opts | ||
| 209 | $opts = defined $opts ? {$opts,@_} : {} ; | ||
| 210 | } | ||
| 211 | my $self = { # following are defaults | ||
| 212 | MKOBJ => $MKOBJ, | ||
| 213 | MKSO => $MKSO, | ||
| 214 | NOCLEAN => undef, | ||
| 215 | FUNC => undef, | ||
| 216 | JFUNC => undef, | ||
| 217 | # TYPE => 'double', | ||
| 218 | NOSO => undef, | ||
| 219 | CSRC => undef, | ||
| 220 | CTOP => undef, | ||
| 221 | DIR => tempdir( CLEANUP => !$opts->{NOCLEAN} ), # where the .so is built | ||
| 222 | TESTSYNTAX => undef, | ||
| 223 | LIBHANDLE => undef, | ||
| 224 | FVERBOSE => 0, | ||
| 225 | }; | ||
| 226 | foreach ( keys %$opts ) { # replace defaults | ||
| 227 | if ( exists $self->{$_} ) { | ||
| 228 | $self->{$_} = $opts->{$_}; | ||
| 229 | } | ||
| 230 | else { die "Levmar::Func::new: Unrecognized option to Levmar::Func '$_'";} | ||
| 231 | } | ||
| 232 | return bless $self,$class; | ||
| 233 | } | ||
| 234 | |||
| 235 | # all these attempts at dying and croaking fail. | ||
| 236 | # program does not quit and error messages are not printed | ||
| 237 | # dont know why. It must be some bad bug in the xs code. | ||
| 238 | # If I just print the error message, its fine. | ||
| 239 | # I think it has to do with DESTROY. return does not work either | ||
| 240 | sub DESTROY { | ||
| 241 | my $self = shift; | ||
| 242 | my $cnt = 0; | ||
| 243 | return if defined $self->{NOSO}; | ||
| 244 | if ( defined $self->{LIBHANDLE}) { | ||
| 245 | my ($ret, $err) = close_shared_object_file($self->{LIBHANDLE}); | ||
| 246 | if ( $err ne '' or $ret != 0) { | ||
| 247 | warn "Levmar::Func::DESTROY: Error closing function library:$ret '$err'"; | ||
| 248 | } | ||
| 249 | warn "Error closing function library:$ret '$err'" if $ret != 0; | ||
| 250 | if ( defined $self->{JPOINTER} ) { # call close twice because we got two symbols | ||
| 251 | ($ret, $err) = close_shared_object_file($self->{LIBHANDLE}); | ||
| 252 | warn "Error closing function library:$ret '$err'" if $ret !=0 ; | ||
| 253 | if ( $err ne '' or $ret != 0 ) { | ||
| 254 | warn "Levmar::Func::DESTROY: Error closing function library:$ret '$err'"; | ||
| 255 | } | ||
| 256 | } | ||
| 257 | if ( defined $self->{SJPOINTER} ) { # call close twice because we got two symbols | ||
| 258 | ($ret, $err) = close_shared_object_file($self->{LIBHANDLE}); | ||
| 259 | warn "Error closing function library:$ret '$err'" if $ret !=0 ; | ||
| 260 | if ( $err ne '' or $ret != 0 ) { | ||
| 261 | warn "Levmar::Func::DESTROY: Error closing function library:$ret '$err'"; | ||
| 262 | } | ||
| 263 | } | ||
| 264 | if ( defined $self->{SFPOINTER} ) { # call close twice because we got two symbols | ||
| 265 | ($ret, $err) = close_shared_object_file($self->{LIBHANDLE}); | ||
| 266 | warn "Error closing function library:$ret '$err'" if $ret !=0 ; | ||
| 267 | if ( $err ne '' or $ret != 0 ) { | ||
| 268 | warn "Levmar::Func::DESTROY: Error closing function library:$ret '$err'"; | ||
| 269 | } | ||
| 270 | } | ||
| 271 | } | ||
| 272 | if (not defined $self->{NOCLEAN} and defined $self->{SONAME} ) { | ||
| 273 | foreach my $k ( qw ( SONAME )) { # clean so file | ||
| 274 | my $fn = $self->{$k}; | ||
| 275 | if (-e $fn) { | ||
| 276 | if ( not -f $fn ) { | ||
| 277 | warn "Levmar::Func::DESTROY: Refusing to remove non-file '$fn'"; | ||
| 278 | } | ||
| 279 | else { | ||
| 280 | $cnt = munlink $fn; | ||
| 281 | if ( $cnt < 1 ) { | ||
| 282 | warn "Levmar::Func::DESTROY: failed removing '$fn' (lib probably open)"; | ||
| 283 | } | ||
| 284 | else { | ||
| 285 | # warn "Levmar::Func::DESTROY: SUCCESS removing '$fn'"; | ||
| 286 | } | ||
| 287 | } | ||
| 288 | } | ||
| 289 | } | ||
| 290 | } | ||
| 291 | } | ||
| 292 | |||
| 293 | sub levmar_func { my $self = new PDL::Fit::Levmar::Func(@_); | ||
| 294 | my @err; | ||
| 295 | if ( defined $self->{FUNC} and $self->{FUNC} =~ /CODE/ ) { | ||
| 296 | $self->{NOSO} = 1; # bad way to handle this | ||
| 297 | $self->{FPOINTER} = 0; # test for this in levmar | ||
| 298 | } | ||
| 299 | else { | ||
| 300 | if ( not defined $self->{FUNC} and not defined $self->{CSRC} ) { | ||
| 301 | die "Levmar::Func::lemar_func: neither FUNC nor CSRC defined"; | ||
| 302 | } | ||
| 303 | @err = $self->make_ccode(); | ||
| 304 | return @err if $err[0] == -1; | ||
| 305 | $self->make_build_names(); | ||
| 306 | $self->write_ccode(); | ||
| 307 | if ( not $self->make_shared_object_file() ) { | ||
| 308 | die "Levmar::Func::levmar_func: Compilation failed."; | ||
| 309 | } | ||
| 310 | if ( not $self->load_fit_library() ) { | ||
| 311 | die "Levmar::Func::levmar_func: Loading dynamic library failed."; | ||
| 312 | } | ||
| 313 | $self->clean_files() unless defined $self->{NOCLEAN}; | ||
| 314 | } | ||
| 315 | if ( defined $self->{JFUNC} ) { # probably won't get called | ||
| 316 | if ( $self->{JFUNC} =~ /CODE/ ) { | ||
| 317 | $self->{NOSO} = 1; | ||
| 318 | } | ||
| 319 | else { | ||
| 320 | die "Levmar::Func::levmar_func option JFUNC must be a ref to a perl sub"; | ||
| 321 | } | ||
| 322 | } | ||
| 323 | return $self; | ||
| 324 | } | ||
| 325 | |||
| 326 | sub make_build_names { | ||
| 327 | my $self = shift; | ||
| 328 | die "Levmar::Func::levmar_func: no name for function" | ||
| 329 | unless defined $self->{NAME}; | ||
| 330 | my $q = $self->{DIR}; | ||
| 331 | if ( not -d $q ) { | ||
| 332 | die "Levmar::Func::levmar_func: Can't make directory (folder) '$q'" | ||
| 333 | unless File::Path::mkpath( $q ); | ||
| 334 | } | ||
| 335 | $self->{FNAME} = $self->{NAME} unless exists $self->{FNAME}; # base file name | ||
| 336 | my $n = $self->{FNAME}; | ||
| 337 | my $srcn = catfile( $q , $n . $PDL::Fit::Levmar::Func::SRC_EXT); | ||
| 338 | my $son = catfile($q ,$n . "." . $SO); | ||
| 339 | my $objn = catfile($q , $n . $OBJ_EXT); | ||
| 340 | $self->{SRCNAME} = $srcn; | ||
| 341 | $self->{SONAME} = $son; | ||
| 342 | $self->{OBJNAME} = $objn; | ||
| 343 | } | ||
| 344 | |||
| 345 | # change this to identity, can remove sometime | ||
| 346 | sub mfqp { | ||
| 347 | my ($self,$key) = @_; | ||
| 348 | return $self->{$key}; | ||
| 349 | } | ||
| 350 | |||
| 351 | # substitue string $new for $old when it | ||
| 352 | # occurs after 'void'. | ||
| 353 | sub subst_func_name { | ||
| 354 | my ($sref,$old,$new) = @_; | ||
| 355 | $$sref =~ s/(\W)void\s+$old/$1void $new/; | ||
| 356 | } | ||
| 357 | |||
| 358 | # Write c source file from function definition. | ||
| 359 | # Entire definition is given as a string, or the | ||
| 360 | # filename , ending in $LPPEXT is given | ||
| 361 | # Copying the source strings around, inefficient. should use | ||
| 362 | # refs | ||
| 363 | sub make_ccode { | ||
| 364 | my $self = shift; | ||
| 365 | my @err; | ||
| 366 | |||
| 367 | if ( not defined $self->{CSRC} ) { # is FUNC really c code? | ||
| 368 | if (not $self->{FUNC} =~ /$LPPEXT$/o # is not a $LPPEXT file | ||
| 369 | and ( $self->{FUNC} =~ /\.c$/ or # is a .c file | ||
| 370 | # doesnt have function def | ||
| 371 | not $self->{FUNC} =~ /(^|\n)\s*function(\s+\w+|\s*)\n/) ) { | ||
| 372 | $self->{CSRC} = $self->{FUNC}; | ||
| 373 | } | ||
| 374 | } | ||
| 375 | if ( defined $self->{CSRC} ) { | ||
| 376 | if ( $self->{CSRC} =~ /\.c$/ ) { | ||
| 377 | my $cf = $self->{CSRC}; | ||
| 378 | local (*CHAND); | ||
| 379 | local $/; | ||
| 380 | open CHAND, "<$cf" or die "Levmar::Func::make_code : Can't open C src file '$cf'"; | ||
| 381 | $self->{CCODE} = <CHAND>; | ||
| 382 | close(CHAND); | ||
| 383 | } | ||
| 384 | else { # inefficient, but it works | ||
| 385 | $self->{CCODE} = $self->{CSRC}; | ||
| 386 | } | ||
| 387 | my ($funcname, $jacname) = $self->get_funcs_from_c(); | ||
| 388 | $jacname = '' unless defined $jacname; | ||
| 389 | my ($sfuncname, $sjacname); | ||
| 390 | $sfuncname = 's' . $funcname; | ||
| 391 | $sjacname = 's' . $jacname if $jacname; | ||
| 392 | my $doublecode = $self->{CCODE}; | ||
| 393 | my $floatcode = $doublecode; | ||
| 394 | subst_func_name(\$floatcode, $funcname, $sfuncname); | ||
| 395 | subst_func_name(\$floatcode, $jacname, $sjacname) if $jacname; | ||
| 396 | if ($^O =~ /MSWin32/i) { | ||
| 397 | my $export = ' __declspec (dllexport) '; | ||
| 398 | subst_func_name(\$doublecode, $funcname, $export . $funcname); | ||
| 399 | subst_func_name(\$floatcode, $sfuncname, $export . $sfuncname); | ||
| 400 | subst_func_name(\$doublecode, $jacname, $export . $jacname) if $jacname; | ||
| 401 | subst_func_name(\$floatcode, $sjacname, $export . $sjacname) if $jacname; | ||
| 402 | } | ||
| 403 | # if ( /^\s*void\s+([\w]+)/ ) | ||
| 404 | $self->{CCODE} = | ||
| 405 | " | ||
| 406 | #define FLOAT double | ||
| 407 | " . $doublecode . | ||
| 408 | " | ||
| 409 | #undef FLOAT | ||
| 410 | #define FLOAT float | ||
| 411 | " . $floatcode; | ||
| 412 | $self->{NAME} = $funcname unless exists $self->{NAME}; | ||
| 413 | $self->{JACNAME} = $jacname unless exists $self->{JACNAME}; | ||
| 414 | return (1); # don't use FUNC even if its there. | ||
| 415 | } | ||
| 416 | elsif ( defined $self->{FUNC} ) { | ||
| 417 | my $st = $self->{FUNC}; | ||
| 418 | if ($st =~ /$LPPEXT$/o) { # file, not string | ||
| 419 | local (*FUNCHAND); | ||
| 420 | local $/; | ||
| 421 | open FUNCHAND, "<$st" or die "Levmar::Func::make_ccode : Can't open def file '$st'"; | ||
| 422 | $st = <FUNCHAND>; | ||
| 423 | close(FUNCHAND); | ||
| 424 | } | ||
| 425 | my ($funcname,$jacname,$ccode); | ||
| 426 | my @ret = generate_C_source($self, $st); | ||
| 427 | if ( $ret[0] eq '-1' ) { # stop warnings with == | ||
| 428 | my $r; | ||
| 429 | # ($r, $funcname, $jacname, $code, @err) = @ret; | ||
| 430 | return @ret; | ||
| 431 | } | ||
| 432 | else { | ||
| 433 | ($funcname,$jacname,$ccode) = @ret; | ||
| 434 | } | ||
| 435 | $self->{CCODE} = $ccode; | ||
| 436 | $self->{NAME} = $funcname unless exists $self->{NAME}; | ||
| 437 | $self->{JACNAME} = $jacname unless exists $self->{JACNAME}; | ||
| 438 | return (1); | ||
| 439 | } | ||
| 440 | return (-1, "Can't make C code with no defintion or source file"); | ||
| 441 | } | ||
| 442 | |||
| 443 | # jacobian function name **must** start with 'jac' if using pure c code | ||
| 444 | sub get_funcs_from_c { | ||
| 445 | my $self = shift; | ||
| 446 | my @lines = split ("\n",$self->{CCODE}); | ||
| 447 | my ($fname, $jacname) = (undef, undef); | ||
| 448 | foreach (@lines ) { | ||
| 449 | if ( /^\s*void\s+([\w]+)/ ) { # prototype begins with 'void' return type | ||
| 450 | my $fn = $1; | ||
| 451 | if ($fn =~ /^jac/ ) { | ||
| 452 | $jacname = $fn; | ||
| 453 | } | ||
| 454 | else { | ||
| 455 | $fname = $fn; | ||
| 456 | } | ||
| 457 | } | ||
| 458 | } | ||
| 459 | if ( not defined $fname or $fname eq '' ) { # ok if jacname == undef | ||
| 460 | die "Levmar::Func::get_funcs_from_c : Can't find function name from c code"; | ||
| 461 | } | ||
| 462 | return ($fname, $jacname); | ||
| 463 | } | ||
| 464 | |||
| 465 | sub write_ccode { | ||
| 466 | my $self = shift; | ||
| 467 | local (*CCODEH); | ||
| 468 | return unless exists $self->{CCODE}; | ||
| 469 | if ( not defined $self->{NAME} ) { | ||
| 470 | die "Levmar::Func::write_ccode : Can't write C code for function with no name"; | ||
| 471 | } | ||
| 472 | my $srcn = $self->{SRCNAME}; | ||
| 473 | open CCODEH, ">$srcn" or die "Levmar::Func::write_ccode : Can't open C source file '$srcn' for writing"; | ||
| 474 | print CCODEH $self->{CTOP}, "\n" if defined $self->{CTOP}; | ||
| 475 | print CCODEH $self->{CCODE}; | ||
| 476 | close CCODEH; | ||
| 477 | } | ||
| 478 | |||
| 479 | sub make_shared_object_file { | ||
| 480 | my $self = shift; | ||
| 481 | my $ret; | ||
| 482 | my $srcn1 = $self->{SRCNAME}; | ||
| 483 | my $fname1 = $self->{FNAME}; | ||
| 484 | my $i = 0; | ||
| 485 | my $max = 20; # allow $max renamed files | ||
| 486 | while ( -f $self->{SONAME} ) { # this is important to avoid user side bugs | ||
| 487 | $i++; | ||
| 488 | my $so = $self->{SONAME}; | ||
| 489 | $self->{FNAME} = $fname1 . $i; | ||
| 490 | $self->make_build_names(); | ||
| 491 | # warn "Levmar::Func::make_shared_object_file: $so exists. Trying " . $self->{SONAME}; | ||
| 492 | last if $i > $max; | ||
| 493 | } | ||
| 494 | my $srcn = $self->{SRCNAME}; | ||
| 495 | my $objn = $self->{OBJNAME}; | ||
| 496 | my $sobjn = $self->{SONAME}; | ||
| 497 | my $fname = $self->{FNAME}; | ||
| 498 | if ( $i > $max ){ | ||
| 499 | die 'Levmar::Func::make_shared_object_file: Too many renamed files in '. $self->{DIR} . ' try cleaning.'; | ||
| 500 | } | ||
| 501 | elsif ($i > 0) { | ||
| 502 | if ( rename($srcn1,$srcn) == 0 ) { | ||
| 503 | die "Levmar::Func::make_shared_object_file: Faile to rename $srcn1 to $srcn"; | ||
| 504 | } | ||
| 505 | } | ||
| 506 | # my $mkobjstr = $self->{MKOBJ} ." -o $objn $srcn"; | ||
| 507 | my $mkobjstr = $self->{MKOBJ}; | ||
| 508 | $mkobjstr =~ s/\%c/$srcn/g; | ||
| 509 | $mkobjstr =~ s/\%o/$objn/g; | ||
| 510 | $ret = do_system($mkobjstr, $self); # compile c to o | ||
| 511 | deb "Levmar::Func::make_shared_object_file: compile returned '$ret'"; | ||
| 512 | if ( $ret != 0) { # zero is success | ||
| 513 | warn "Levmar::Func::make_shared_object_file: Compiling '$srcn' failed."; | ||
| 514 | return 0; | ||
| 515 | } | ||
| 516 | $self->{MKOBJSTR} = $mkobjstr; | ||
| 517 | my $sostr = $self->{MKSO}; | ||
| 518 | $sostr =~ s/\%s/$sobjn/g; | ||
| 519 | $sostr =~ s/\%o/$objn/g; | ||
| 520 | $ret = do_system($sostr, $self); # make so from o | ||
| 521 | deb "Levmar::Func::make_shared_object_file: '$sostr' make so from o returned '$ret'"; | ||
| 522 | if ( 0 != $ret ) { | ||
| 523 | warn "Levmar::Func::make_shared_object_file: Making shared library '$sobjn' failed."; | ||
| 524 | return 0; | ||
| 525 | } | ||
| 526 | $self->{SOSTR} = $sostr; | ||
| 527 | } | ||
| 528 | |||
| 529 | #### Some methods to return information | ||
| 530 | # get compiler strings | ||
| 531 | sub get_cc_str { | ||
| 532 | my $self = shift; | ||
| 533 | return ($self->{MKOBJSTR}, $self->{SOSTR}); | ||
| 534 | } | ||
| 535 | |||
| 536 | sub get_fpoint { | ||
| 537 | my $self = shift; | ||
| 538 | $self->{FPOINTER}; | ||
| 539 | } | ||
| 540 | |||
| 541 | sub get_jacpoint { | ||
| 542 | my $self = shift; | ||
| 543 | $self->{JPOINTER}; | ||
| 544 | } | ||
| 545 | |||
| 546 | sub get_fit_pointers { | ||
| 547 | my $self = shift; | ||
| 548 | ($self->{FPOINTER}, | ||
| 549 | $self->{SFPOINTER}, | ||
| 550 | $self->{JPOINTER}, | ||
| 551 | $self->{SJPOINTER}); | ||
| 552 | } | ||
| 553 | |||
| 554 | sub get_file_names { | ||
| 555 | my $self = shift; | ||
| 556 | return ($self->mfqp('SRCNAME'), | ||
| 557 | $self->mfqp('OBJNAME'), | ||
| 558 | $self->mfqp('SONAME')); | ||
| 559 | } | ||
| 560 | |||
| 561 | # remove generated o, and C files | ||
| 562 | # Well, don't remove so file. | ||
| 563 | sub clean_files { | ||
| 564 | my $self = shift; | ||
| 565 | my $cnt = 0; | ||
| 566 | my $srcn = $self->mfqp('SRCNAME'); # fully qualified names | ||
| 567 | my $objn = $self->mfqp('OBJNAME'); | ||
| 568 | foreach my $k ( qw ( OBJNAME )) { # soname needs to be there | ||
| 569 | my $fn = $self->mfqp($k); | ||
| 570 | if (-e $fn) { | ||
| 571 | if ( not -f $fn ) { | ||
| 572 | warn "Levmar::Func::clean_files : Object file '$fn' not a file. Not deleting"; | ||
| 573 | } | ||
| 574 | else { | ||
| 575 | $cnt = munlink $fn; | ||
| 576 | if ( $cnt < 1) { | ||
| 577 | warn "Levmar::Func::clean_files : cnt $cnt: Failed to delete object file '$fn'"; | ||
| 578 | } | ||
| 579 | } | ||
| 580 | } | ||
| 581 | } | ||
| 582 | if ( -e $srcn ) { | ||
| 583 | if ( not -f $srcn ) { | ||
| 584 | warn "Levmar::Func::clean_files: Source file '$srcn' not a file. Not deleting"; | ||
| 585 | } | ||
| 586 | open my $srch , '<' , | ||
| 587 | $srcn or die "Levmar::Func::clean_files : Can't open C source file '$srcn' for reading"; | ||
| 588 | my $closed = 0; | ||
| 589 | # weird stack bug present when using implicit $_ rather than $line. | ||
| 590 | # A function that calls levmar_func sometimes has an item in arg stack | ||
| 591 | # replaced by the first line read here. | ||
| 592 | while (my $line = <$srch>) { | ||
| 593 | next if $line =~ /^\s*$/; | ||
| 594 | if ($line =~ /\/\* This file automatically/) { | ||
| 595 | close($srch); | ||
| 596 | $closed = 1; | ||
| 597 | $cnt = 0; | ||
| 598 | $cnt = munlink $srcn; | ||
| 599 | warn "Levmar::Func::clean_files: cnt $cnt: Failed to delete source file '$srcn'" | ||
| 600 | if $cnt < 1; | ||
| 601 | last; | ||
| 602 | } | ||
| 603 | last; # only check up to first non-blank line | ||
| 604 | } | ||
| 605 | close($srch) unless $closed == 1; | ||
| 606 | } | ||
| 607 | } | ||
| 608 | |||
| 609 | # Improve effiency after this is well tested by getting all four | ||
| 610 | # symbols at once. | ||
| 611 | |||
| 612 | sub load_fit_library { | ||
| 613 | my $self = shift; | ||
| 614 | my $so = $self->mfqp('SONAME'); | ||
| 615 | my $fn = $self->{NAME}; | ||
| 616 | my $jn = $self->{JACNAME}; | ||
| 617 | die "Levmar::Func::load_fit_library : Can't find dynamic library file '$so'" unless -e $so; | ||
| 618 | my ($func_pointer, $jac_pointer, $lib_handle, $error_message)= | ||
| 619 | (0,0,0,0); | ||
| 620 | my ($sfunc_pointer, $sjac_pointer) = # single precision | ||
| 621 | (0,0); | ||
| 622 | ($func_pointer, $lib_handle, $error_message) = | ||
| 623 | open_and_find($so,$fn); | ||
| 624 | ($sfunc_pointer, $lib_handle, $error_message) = # single precision | ||
| 625 | open_and_find($so,'s'.$fn); | ||
| 626 | if ( $error_message ne '' ) { | ||
| 627 | warn "Levmar::Func::load_fit_library: Failed to load dynamic library '$so'\n". | ||
| 628 | " and find fit func '$fn'.\n dlopen:'$error_message'"; | ||
| 629 | return 0; | ||
| 630 | } | ||
| 631 | if ( defined $jn and $jn ne '') { | ||
| 632 | ($jac_pointer, $lib_handle, $error_message) = | ||
| 633 | open_and_find($so,$jn); | ||
| 634 | if ( $error_message ne '' ) { | ||
| 635 | warn "Levmar::Func::load_fit_library: Failed to load dynamic library '$so' and find jacobian '$jn'.\n". | ||
| 636 | " dlopen:'$error_message' "; | ||
| 637 | } | ||
| 638 | else { | ||
| 639 | $self->{JPOINTER} = $jac_pointer; | ||
| 640 | } | ||
| 641 | ($sjac_pointer, $lib_handle, $error_message) = | ||
| 642 | open_and_find($so, 's' . $jn); | ||
| 643 | if ( $error_message ne '' ) { | ||
| 644 | warn "Levmar::Func::load_fit_library: Failed to load dynamic library '$so' and find jacobian '$jn'.\n". | ||
| 645 | " dlopen:'$error_message' "; | ||
| 646 | } | ||
| 647 | else { | ||
| 648 | $self->{SJPOINTER} = $sjac_pointer; | ||
| 649 | } | ||
| 650 | |||
| 651 | } | ||
| 652 | $self->{FPOINTER} = $func_pointer; | ||
| 653 | $self->{SFPOINTER} = $sfunc_pointer; | ||
| 654 | $self->{LIBHANDLE} = $lib_handle; | ||
| 655 | } | ||
| 656 | |||
| 657 | # End of methods | ||
| 658 | # (Actually there are some more methods near the bottom | ||
| 659 | # that are close to the corresponding pp_defs) | ||
| 660 | ######################################## | ||
| 661 | # Non-methods below here | ||
| 662 | |||
| 663 | # should return 0 on success | ||
| 664 | sub do_system { | ||
| 665 | my ($c,$f) = @_; | ||
| 666 | print STDERR $c,"\n" if $f->{FVERBOSE}; | ||
| 667 | system $c; | ||
| 668 | } | ||
| 669 | |||
| 670 | sub munlink { | ||
| 671 | my $f = shift; | ||
| 672 | my $cnt; | ||
| 673 | my $sf = "'$f'"; | ||
| 674 | if ( not -e $f ) { | ||
| 675 | prwarn "munlink: $sf does not exist"; | ||
| 676 | return 0; | ||
| 677 | } | ||
| 678 | if ( not -f $f ) { | ||
| 679 | prwarn "munlink: $sf is not a file. Not removing."; | ||
| 680 | return 0; | ||
| 681 | } | ||
| 682 | $cnt = unlink $f; | ||
| 683 | if (not $cnt > 0) { | ||
| 684 | prwarn "munlink: Can't unlink exisiting file $sf"; | ||
| 685 | } | ||
| 686 | return $cnt; | ||
| 687 | } | ||
| 688 | |||
| 689 | # Turn a function description into C source | ||
| 690 | # !care! I disabled NiceSlice, because it erroneously saw | ||
| 691 | # slicing in this piddle-less routine. But I was not using it | ||
| 692 | # anyway. (parser is rewritten, maybe not a problem anymore.) | ||
| 693 | sub generate_C_source { | ||
| 694 | my ($self, $fdef) = @_; | ||
| 695 | my $funcname = ''; # this one is returned | ||
| 696 | my $jacname = ''; # this one is returned | ||
| 697 | # Do this first now send t -> t[i], x -> x[i], p3 -> p[3] | ||
| 698 | # Probably a better way than doing this in a loop | ||
| 699 | # but overlapping matchin patterns do not work with global flag | ||
| 700 | # t --> t[i] | ||
| 701 | |||
| 702 | # I tried to substitutions on $fdef vs on each line. Not much difference. | ||
| 703 | my $defs = {}; | ||
| 704 | my ($lines,$def, $loopf, $noloopf, $name); | ||
| 705 | my (@flines) = split(/\n/, $fdef); | ||
| 706 | $lines = []; | ||
| 707 | my $prefunc = $lines;# lines before any func def | ||
| 708 | foreach my $oneline (@flines) { #break code into functions | ||
| 709 | next if $oneline =~ /^\s*end\s+(function|jacobian)\s*/; | ||
| 710 | if ( $oneline =~ /^\s*(function|jacobian)\s*(\w*)\s*$/ ) { # func declaration | ||
| 711 | $name = $2; | ||
| 712 | $name = "func" unless $name ne ''; | ||
| 713 | my $type = $1; | ||
| 714 | $name = "jac" . $name if $type eq 'jacobian' and not $name =~ /^jac/ ; | ||
| 715 | $def = $defs->{$name} = {}; | ||
| 716 | $def->{type} = $type; | ||
| 717 | $lines = []; | ||
| 718 | $def->{loop} = $lines; | ||
| 719 | $noloopf = 0; | ||
| 720 | $loopf = 0; | ||
| 721 | next; | ||
| 722 | } | ||
| 723 | if ( $oneline =~ /^\s*noloop/ ) { # don't want any implicit loop | ||
| 724 | $def->{preloop} = $lines; | ||
| 725 | $def->{loop} = undef; | ||
| 726 | $noloopf = 1; | ||
| 727 | next; | ||
| 728 | } | ||
| 729 | elsif ( $oneline =~ /^\s*loop/ ) { # start implicit loop | ||
| 730 | $def->{preloop} = $lines; | ||
| 731 | $lines = []; | ||
| 732 | $def->{loop} = $lines; | ||
| 733 | $loopf = 1; | ||
| 734 | next; | ||
| 735 | } | ||
| 736 | else { | ||
| 737 | push @$lines, "" .$oneline ."\n"; | ||
| 738 | } | ||
| 739 | } | ||
| 740 | |||
| 741 | |||
| 742 | if ( defined $self->{TESTSYNTAX} ) { | ||
| 743 | my $st = ''; | ||
| 744 | if ( @$prefunc > 0) { | ||
| 745 | $st .= ":PREFUNC: lines:" . scalar(@{$prefunc}). "\n"; | ||
| 746 | $st .= join("\n",@{$prefunc}). "\n"; | ||
| 747 | } | ||
| 748 | foreach my $name ( keys %$defs ) { | ||
| 749 | my $d = $defs->{$name}; | ||
| 750 | $st .= "NAME: $name, TYPE: " . $d->{type}; | ||
| 751 | if (defined $d->{preloop}) { | ||
| 752 | $st .= ":PRELOOP: lines:" . scalar(@{$d->{preloop}}). "\n"; | ||
| 753 | $st .= join("\n",@{$d->{preloop}}). "\n"; | ||
| 754 | } | ||
| 755 | if (defined $d->{loop}) { | ||
| 756 | $st .= ":LOOP: lines:" . scalar(@{$d->{loop}}). "\n"; | ||
| 757 | $st .= join("\n",@{$d->{loop}}). "\n"; | ||
| 758 | } | ||
| 759 | } | ||
| 760 | $self->{SYNTAXRESULTS} = $st; | ||
| 761 | } | ||
| 762 | |||
| 763 | my $end_function = "\}\n\n"; | ||
| 764 | my $end_loop = " \}\n"; | ||
| 765 | my $start_loop = { 'jacobian' => " for(i=j=0; i<n; ++i){\n", | ||
| 766 | 'function' => " for(i=0; i<n; ++i){\n" }; | ||
| 767 | |||
| 768 | # my $dtype = $self->{TYPE}; | ||
| 769 | my $dtype = 'FLOAT'; | ||
| 770 | my $start_function = { | ||
| 771 | 'jacobian' => "void JACFUNNAME($dtype *p, $dtype *d, int m, int n, $dtype *t)\n\{\n". | ||
| 772 | " int i,j;\n", | ||
| 773 | # " $dtype *t = ($dtype *) data;\n", | ||
| 774 | 'function' => "void FITFUNNAME($dtype *p, $dtype *x, int m, int n, $dtype *t)\n\{\n". | ||
| 775 | " int i;\n" }; | ||
| 776 | # " $dtype *t = ($dtype *) data;\n" }; | ||
| 777 | my $kode = | ||
| 778 | "/* This file automatically created and deleted. Do not edit. */\n" . | ||
| 779 | "#include<math.h>\n#include <stdio.h>\n\n" . | ||
| 780 | join('',@$prefunc); | ||
| 781 | |||
| 782 | my $kode1 = ''; | ||
| 783 | |||
| 784 | foreach my $name ( keys %$defs ) { | ||
| 785 | my $d = $defs->{$name}; | ||
| 786 | my ($loop, $preloop, $loopcode); | ||
| 787 | my $type = $d->{type}; | ||
| 788 | $funcname = $name if $type eq 'function'; | ||
| 789 | $jacname = $name if $type eq 'jacobian'; | ||
| 790 | if ( defined $d->{loop} ) { | ||
| 791 | $loopcode = join("", @{$d->{loop} }); | ||
| 792 | if ( $loopcode =~ /^\s*$/ ) { # loop code is whitespace | ||
| 793 | $loopcode = undef; # so don't print it | ||
| 794 | } | ||
| 795 | else { | ||
| 796 | $loop = $d->{loop}; | ||
| 797 | } | ||
| 798 | } | ||
| 799 | $preloop = $d->{preloop} if defined $d->{preloop}; | ||
| 800 | |||
| 801 | # $kode1 .= sprintf($start_function->{$type},$name); | ||
| 802 | $kode1 .= sprintf($start_function->{$type}, ''); | ||
| 803 | if (defined $preloop ) { | ||
| 804 | foreach (@$preloop) { | ||
| 805 | # p5 --> p[5] | ||
| 806 | while ($_ =~ s/([^\w]|^)p(\d+)([^\w\[])/$1p[$2]$3/g){} | ||
| 807 | if ( $type eq 'function' ) { | ||
| 808 | # x7 -- > x[7] | ||
| 809 | while($_ =~ s/([^\w]|^)x(\d+)([^\w\[])/$1x[$2]$3/g){} | ||
| 810 | } | ||
| 811 | elsif ( $type eq 'jacobian' ) { | ||
| 812 | # d3[6] -> d[ 3+ (n-1)*6], no! [3+m*6] is correct | ||
| 813 | while($_ =~ /([^\w]|^)d(\d+)\[(\d+)\]/ ) { | ||
| 814 | my $i = "$2+m*$3" ; | ||
| 815 | $_ =~ s/([^\w]|^)d\d+\[\d+\]/$1d[$i]/; | ||
| 816 | } | ||
| 817 | } | ||
| 818 | } | ||
| 819 | $kode1 .= join('',@$preloop); | ||
| 820 | } | ||
| 821 | if ( defined $loop ) { | ||
| 822 | $kode1 .= $start_loop->{$type}; | ||
| 823 | foreach ( @$loop ) { | ||
| 824 | # t -- > t[i] | ||
| 825 | while($_ =~ s/([^\w]|^)t([^\w\[])/$1t[i]$2/g){} | ||
| 826 | # p5 --> p[5] | ||
| 827 | while ($_ =~ s/([^\w]|^)p(\d+)([^\w\[])/$1p[$2]$3/g){} | ||
| 828 | if ( $type eq 'function' ) { | ||
| 829 | # x -> x[i] | ||
| 830 | while($_ =~ s/([^\w]|^)x([^\w\[])/$1x[i]$2/g){} | ||
| 831 | } | ||
| 832 | elsif ( $type eq 'jacobian' ) { | ||
| 833 | if ( /^\s*d\d+\[?i?\]?(.+)/ ) { | ||
| 834 | my $line = $1; | ||
| 835 | $line =~ s/\s*//; # strip space | ||
| 836 | $_ = " d[j++] $line\n"; | ||
| 837 | } | ||
| 838 | } | ||
| 839 | else { | ||
| 840 | warn "Levmar::Func::generate_C_source: Function type neither 'function' nor 'jacobian'"; | ||
| 841 | } | ||
| 842 | } | ||
| 843 | $kode1 .= join('',@$loop) . $end_loop; | ||
| 844 | } | ||
| 845 | $kode1 .= $end_function; | ||
| 846 | } #foreach my $name .. | ||
| 847 | my $export = $^O =~ /MSWin32/i ? '__declspec (dllexport)' : ''; | ||
| 848 | $kode .= " | ||
| 849 | #define FLOAT double | ||
| 850 | #define JACFUNNAME $export $jacname | ||
| 851 | #define FITFUNNAME $export $funcname | ||
| 852 | " . $kode1 . | ||
| 853 | " | ||
| 854 | #undef FLOAT | ||
| 855 | #undef JACFUNNAME | ||
| 856 | #undef FITFUNNAME | ||
| 857 | #define FLOAT float | ||
| 858 | #define JACFUNNAME $export s$jacname | ||
| 859 | #define FITFUNNAME $export s$funcname | ||
| 860 | " . $kode1; | ||
| 861 | return ($funcname,$jacname,$kode); | ||
| 862 | } # end sub generate_C_source { | ||
| 863 | |||
| 864 | # load shared library with fit function and find a symbol | ||
| 865 | # (pointer to fit function or jacobian in our case.) | ||
| 866 | # $lib_name : name of library file , eg "./func.so" | ||
| 867 | # $func_name : name of function, eg "square" | ||
| 868 | # $func_pointer : value of C level pointer to $func_name, cast to int | ||
| 869 | # $lib_handle : same, but for pointer to library | ||
| 870 | sub open_and_find { | ||
| 871 | my ($lib_name, $func_name) = @_; | ||
| 872 | my $lib_handle = 0; | ||
| 873 | my $func_pointer = 0; | ||
| 874 | my $nchar = 1000; # avoid buffer overun by sending this number to memccpy | ||
| 875 | my $error_message = "." x $nchar; | ||
| 876 | deb "call_open_and_find for lib $lib_name, func $func_name."; | ||
| 877 | # print STDERR "Nargs 1 ", scalar(@_) , "\n"; | ||
| 878 | my @res = call_open_and_find($lib_name, $func_name, $lib_handle, | ||
| 879 | $func_pointer, $error_message , $nchar); | ||
| 880 | # print STDERR "Nargs 2 ", scalar(@_) , "\n"; | ||
| 881 | # print STDERR ("call_open_and_find returned ", scalar(@res), " items\n."); | ||
| 882 | # print STDERR (@res),"\n"; | ||
| 883 | return ($func_pointer, $lib_handle, $error_message); | ||
| 884 | # return ($func_pointer, $lib_handle, ''); | ||
| 885 | } | ||
| 886 | |||
| 887 | sub close_shared_object_file { | ||
| 888 | my ($lib_handle) = @_; | ||
| 889 | my $nchar = 1000; # avoid buffer overun by sending this number to memccpy | ||
| 890 | my $error_message = "." x $nchar; | ||
| 891 | my $ret = 0; | ||
| 892 | deb "Closing so file."; | ||
| 893 | # print STDERR "Nargs 3 ", scalar(@_) , "\n"; | ||
| 894 | my @res = call_close_shared_object_file( $lib_handle, $ret, $error_message, $nchar); | ||
| 895 | # print STDERR "Nargs 4 ", scalar(@_) , "\n"; | ||
| 896 | # print STDERR ("close shared returned ", scalar(@res), " items\n."); | ||
| 897 | # print STDERR (@res),"\n"; | ||
| 898 | return ($ret, $error_message); | ||
| 899 | } | ||
| 900 | |||
| 901 | #----------------------------------------- | ||
| 902 | # Some methods to call the functions | ||
| 903 | |||
| 904 | # evaluate the function. | ||
| 905 | # Given parameters p and coordiante pdl t, | ||
| 906 | # return x(t;p) with x->dims == t->dims. | ||
| 907 | # The functions don't really have to have | ||
| 908 | # this form, but this is what we assume for | ||
| 909 | # this call. | ||
| 910 | sub call { | ||
| 911 | my $self = shift; | ||
| 912 | my ($p,$t) = @_; # pdls | ||
| 913 | my $x = zeroes($t); | ||
| 914 | if ($self->{FPOINTER} == 0) { | ||
| 915 | warn "Levmar::Func::call : No function loaded."; | ||
| 916 | return PDL->null; | ||
| 917 | } | ||
| 918 | _callf(topdl($p),$t,$x, $self->{FPOINTER}); | ||
| 919 | return($x); | ||
| 920 | } | ||
| 921 | |||
| 922 | # call jacobian and get back 2-d pdl | ||
| 923 | # return jac(m,n) = jacfunc(p(m),t(n)) | ||
| 924 | sub jac_of_t { | ||
| 925 | my $self = shift; | ||
| 926 | my ($p,$t) = @_; # pdls | ||
| 927 | if ($self->{JPOINTER} == 0) { | ||
| 928 | warn "Levmar::Func::jac_of_t: No jacobian function loaded."; | ||
| 929 | return PDL->null; | ||
| 930 | } | ||
| 931 | my @dims = dims($t); | ||
| 932 | my $jac = zeroes($p->type, $p->nelem,@dims); | ||
| 933 | return 0 unless ref($jac) =~ /PDL/; | ||
| 934 | _callj($p,$t,$jac, $self->{JPOINTER}); | ||
| 935 | return($jac); | ||
| 936 | } | ||
| 937 | |||
| 938 | # treat array of derivatives as 1-d, just at the | ||
| 939 | # supplied c routines do. For debugging. | ||
| 940 | # return jac(m*n) = jacfunc(p(m),t(n)) | ||
| 941 | sub jac_of_t1 { | ||
| 942 | my $self = shift; | ||
| 943 | my ($p,$t) = @_; # pdls | ||
| 944 | if ($self->{JPOINTER} == 0) { | ||
| 945 | warn "Levmar::Func::jac_of_t1: No jacobian function loaded."; | ||
| 946 | return PDL->null; | ||
| 947 | } | ||
| 948 | my @dims = dims($t); | ||
| 949 | foreach (@dims) { $_ *= $p->nelem} | ||
| 950 | my $jac = zeroes($p->type, @dims); | ||
| 951 | return 0 unless ref($jac) =~ /PDL/; | ||
| 952 | _callj1($p,$t,$jac, $self->{JPOINTER}); | ||
| 953 | return($jac); | ||
| 954 | } | ||
| 955 | #line 956 "Func.pm" | ||
| 956 | |||
| 957 | *_callf = \&PDL::Fit::Levmar::Func::_callf; | ||
| 958 | |||
| 959 | |||
| 960 | |||
| 961 | |||
| 962 | *_callj = \&PDL::Fit::Levmar::Func::_callj; | ||
| 963 | |||
| 964 | |||
| 965 | |||
| 966 | |||
| 967 | *_callj1 = \&PDL::Fit::Levmar::Func::_callj1; | ||
| 968 | |||
| 969 | |||
| 970 | |||
| 971 | |||
| 972 | |||
| 973 | |||
| 974 | |||
| 975 | #line 158 "func.pd" | ||
| 976 | |||
| 977 | =head1 AUTHORS | ||
| 978 | |||
| 979 | Copyright (C) 2006 John Lapeyre. | ||
| 980 | All rights reserved. There is no warranty. You are allowed | ||
| 981 | to redistribute this software / documentation under certain | ||
| 982 | conditions. For details, see the file COPYING in the PDL | ||
| 983 | distribution. If this file is separated from the PDL distribution, | ||
| 984 | the copyright notice should be included in the file. | ||
| 985 | |||
| 986 | =cut | ||
| 987 | #line 988 "Func.pm" | ||
| 988 | |||
| 989 | # Exit with OK status | ||
| 990 | |||
| 991 | 1; | ||