Diff for /loncom/homework/functionplotresponse.pm between versions 1.25 and 1.26

version 1.25, 2010/11/07 01:57:50 version 1.26, 2010/11/07 11:15:37
Line 553  sub d2dt2_cubic_hermite { Line 553  sub d2dt2_cubic_hermite {
 #  #
 sub array_index {  sub array_index {
    my ($xmin,$xmax,$x)=@_;     my ($xmin,$xmax,$x)=@_;
    return int(($x-$xmin)/($xmax-$xmin)*200.+0.5);     return int(($x-$xmin)/($xmax-$xmin)*400.+0.5);
 }  }
   
 #  #
Line 562  sub array_index { Line 562  sub array_index {
   
 sub index_x {  sub index_x {
    my ($xmin,$xmax,$i)=@_;     my ($xmin,$xmax,$i)=@_;
    return $i*($xmax-$xmin)/200.+$xmin;     return $i*($xmax-$xmin)/400.+$xmin;
 }  
   
 #  
 # Assume function to be linear between array points  
 # Return the two indices and the scale factor  
 #  
   
 sub indices_scale {  
    my ($xmin,$xmax,$x)=@_;  
    my $i=&array_index($xmin,$xmax,$x);  
    my $xr=&index_x($xmin,$xmax,$i);  
    if ($xr<$x) {  
 # Desired x is right of array index  
       if ($i>=200) { return (200,200,0); }  
       return($i,$i+1,200.*($x-$xr)/($xmax-$xmin));  
    } elsif ($xr>$x) {  
 # Desired x is left of array index  
       if ($i<=0) { return (0,0,0); }  
       return($i-1,$i,1.-200.*($xr-$x)/($xmax-$xmin));  
    } else {  
 # Desired x is at array index (unlikely, but ...)  
       if ($i>=200) { return (200,200,0); }  
       if ($i<=0) { return (0,0,0); }  
       return ($i,$i,0);  
    }  
 }  }
   
 #  #
Line 596  sub indices_scale { Line 571  sub indices_scale {
   
 sub func_val {  sub func_val {
    my ($xmin,$xmax,$x)=@_;     my ($xmin,$xmax,$x)=@_;
    my ($il,$ih,$factor)=&indices_scale($xmin,$xmax,$x);     return $Apache::functionplotresponse::func[&array_index($xmin,$xmax,$x)];
    my $fl=$Apache::functionplotresponse::func[$il];  
    my $fh=$Apache::functionplotresponse::func[$ih];  
    unless (defined($fl) || defined($fh)) { return undef; }  
    unless (defined($fl)) { return $fh; }  
    unless (defined($fh)) { return $fl; }  
    return $fl+$factor*($fh-$fl);  
 }  }
   
 #  #
Line 611  sub func_val { Line 580  sub func_val {
   
 sub dfuncdx_val {  sub dfuncdx_val {
    my ($xmin,$xmax,$x)=@_;     my ($xmin,$xmax,$x)=@_;
    my ($il,$ih,$factor)=&indices_scale($xmin,$xmax,$x);     return $Apache::functionplotresponse::dfuncdx[&array_index($xmin,$xmax,$x)];
    my $fl=$Apache::functionplotresponse::dfuncdx[$il];  
    my $fh=$Apache::functionplotresponse::dfuncdx[$ih];  
    unless (defined($fl) || defined($fh)) { return undef; }  
    unless (defined($fl)) { return $fh; }  
    unless (defined($fh)) { return $fl; }  
    return $fl+$factor*($fh-$fl);  
 }  }
   
 #  #
Line 626  sub dfuncdx_val { Line 589  sub dfuncdx_val {
   
 sub d2funcdx2_val {  sub d2funcdx2_val {
    my ($xmin,$xmax,$x)=@_;     my ($xmin,$xmax,$x)=@_;
    my ($il,$ih,$factor)=&indices_scale($xmin,$xmax,$x);     return $Apache::functionplotresponse::d2funcdx2[&array_index($xmin,$xmax,$x)];
    my $fl=$Apache::functionplotresponse::d2funcdx2[$il];  
    my $fh=$Apache::functionplotresponse::d2funcdx2[$ih];  
    unless (defined($fl) || defined($fh)) { return undef; }  
    unless (defined($fl)) { return $fh; }  
    unless (defined($fh)) { return $fl; }  
    return $fl+$factor*($fh-$fl);  
 }  }
   
 #  #
Line 641  sub d2funcdx2_val { Line 598  sub d2funcdx2_val {
   
 sub populate_arrays {  sub populate_arrays {
     my ($id,$xmin,$xmax)=@_;      my ($id,$xmin,$xmax)=@_;
     for (my $i=0; $i<=200; $i++) {      for (my $i=0; $i<=400; $i++) {
        $Apache::functionplotresponse::func[$i]=undef;         $Apache::functionplotresponse::func[$i]=undef;
        $Apache::functionplotresponse::dfuncdx[$i]=undef;         $Apache::functionplotresponse::dfuncdx[$i]=undef;
        $Apache::functionplotresponse::d2funcd2x[$i]=undef;         $Apache::functionplotresponse::d2funcd2x[$i]=undef;
Line 662  sub populate_arrays { Line 619  sub populate_arrays {
                         $env{'form.HWVAL_'.$id.'_'.$label.'P'.$ni.'_y'},                          $env{'form.HWVAL_'.$id.'_'.$label.'P'.$ni.'_y'},
                         $env{'form.HWVAL_'.$id.'_'.$label.'S'.$ni.'_y'});                          $env{'form.HWVAL_'.$id.'_'.$label.'S'.$ni.'_y'});
 # Run in small steps over spline parameter  # Run in small steps over spline parameter
             for (my $t=0; $t<=1; $t+=0.00025) {              for (my $t=0; $t<=1; $t+=0.0001) {
                 my $xi=&array_index($xmin,$xmax,&cubic_hermite($t,@xparms));                  my $xi=&array_index($xmin,$xmax,&cubic_hermite($t,@xparms));
                 if ($xi<$xiold) { return 'no_func'; }                  if ($xi<$xiold) { return 'no_func'; }
                 if (($xi>$xiold) && ($xi>=0) && ($xi<=200)) {                  if (($xi>$xiold) && ($xi>=0) && ($xi<=400)) {
                    if (defined($Apache::functionplotresponse::func[$xi])) { return 'no_func'; }                     if (defined($Apache::functionplotresponse::func[$xi])) { return 'no_func'; }
                    $xiold=$xi;                     $xiold=$xi;
 # Function value  # Function value
Line 786  sub start_functionplotresponse { Line 743  sub start_functionplotresponse {
 }  }
   
 sub compare_rel {  sub compare_rel {
    my ($relationship,$value,$val,$tol)=@_;     my ($relationship,$value,$realval,$tol)=@_;
    if (abs($value-$val)<$tol) { return 1; }  # is the real value defined?
      unless (defined($realval)) {
         if ($relationship eq 'eq') {
            if ($value eq 'undef') {
               return 1;
            } else {
               return 0;
            }
         } elsif ($relationship eq 'ne') {
            if ($value eq 'undef') {
               return 0;
            } else {
               return 1;
            }
         } else {
            return 0;
         }
      }
   
   # it is defined.
      if ($relationship eq 'gt') {
         return ($realval>$value);
      } elsif ($relationship eq 'ge') {
         return ($realval>$value-$tol);
      } elsif ($relationship eq 'lt') {
         return ($realval<$value);
      } elsif ($relationship eq 'le') {
         return ($realval<$value+$tol);
      } elsif ($relationship eq 'ne') {
         return (abs($value-$realval)>$tol);
      } else {
         return (abs($value-$realval)<$tol);
      }
    return 0;     return 0;
 }  }
   
Line 876  sub end_functionplotresponse { Line 865  sub end_functionplotresponse {
         $ymin=(defined($ymin)?$ymin:-10);          $ymin=(defined($ymin)?$ymin:-10);
         my $ymax=&Apache::lonxml::get_param('ymax',$parstack,$safeeval);          my $ymax=&Apache::lonxml::get_param('ymax',$parstack,$safeeval);
         $ymax=(defined($ymax)?$ymax:10);          $ymax=(defined($ymax)?$ymax:10);
         my $tolfunc=($ymax-$ymin)/100.;          my $tolfunc=5.*($ymax-$ymin)/100.;
         my $toldfdx=1;          my $toldfdx=1;
         my $told2fdx2=1;          my $told2fdx2=1;
         if ($xmax>$xmin) {          if ($xmax>$xmin) {

Removed from v.1.25  
changed lines
  Added in v.1.26


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>