NAME
Text::NumericData::Calc - helper package for some calculations
SYNOPSIS
use Text::NumericData::Calc qw(linear_value formula_function);
#inter- or extrapolation with known data points ($x1,$y1) and ($x2,$y2)
$y = linear_value($x,[$x1,$x2],[$y1,$y2]);
my $ff = formula_function($textualformula);
my $value = &$ff(\@datasets);
or, using plain Perl syntax in formula,
my $ff = formula_function($perlformula, {plainperl=>1});
and with verbosity to print the parsed formula
my $ff = formula_function($perlformula, {verbose=>1});
DESCRIPTION
This is a little library for Text::NumericData; it contains routines that in fact are too general to strictly belong to Text::NumericData... but here they are.
Functions
linear_value($x,[$x1,$x2],[$y1,$y2]) -> $y
does simple linear inter-/extrapolation of the $y for a given $x based on two known points. It is needed by the Text::NumericData::File class for, well, interpolation.
formula_function($formula,$plainperl) -> \&function
This takes some formula as text and constructs a function that evaluates this formula. The created function takes three arrays (references) as arguments: A two-dimensional array and two one-dimensional arrays with some data needed for calculation.
A shortcut syntax is provided to be able to quickly write simple formulae without too much noise around it. [n] maps to the value of column n, the first being column 1. [m,n] maps to column n in file m, file 0 being the primary data set, file 1 the first auxilliary one. The extra one-dimensional arrays are accessed via Ci for $C->[i] and Ai for $A->[i].
In short, the formula
[2]*=[1,2] + C0; A0+=[2]
is equivalent to
$fd->[0][1]*=$fd->[1][1] + $C->[0]; $A->[0]+=$fd->[0][1]
in plain Perl, with $fd referring to the value matrix and @{$C} for the constants, @{$A} for the auxilliary values.
Perhaps now you understand why I wanted to abbreviate things to make this workable for shell one-liners. You are always free to write your formula in plain Perl using the indicated variables when setting the second argument to some true value. Generally, the simplified syntax works well for quick one-liners.
There are further shortcuts that translate barewords to data array entries, x for[1], y for [2] and z for [3].
Note that there are no real boundaries to what the generated routine may do. It is implied that you can trust the user input. Think twice before including this functionality unguarded in public web service! You should only allow preconstructed verified input in such a case. But, well, what am I talking about in the age of SQL injection ...
Limiting the scope of operations in the formula to enforce security is not really an option. It would loose functionality. Just think about loops.
parsed_formula($formula, $dataname, $par1name, $par2name) -> $perl_code
is the parser for the $formula that just replaces the different short forms for data and constants with proper PERL code using the last three parameters for the array names. It's used internally by formula_function. If you really want to use it explicitly, then it shouldn't be hard to figure the usage out by looking at the source of formula_function.
TODO
I'd like to add shortcuts for Fortran-like array operations.
[2:] += 1
to increment all columns from the second one,
[2:] *= [1,2:]
to multiply all corresponding columns with factors in second data set (matrix row). This needs more smarts in making things match up. For now, you have to either resort to plain Perl (in case Perl's array syntax does the trick) or something like
for $i (0..4){ [$i] = 2 }
wich translates to
for $i (0..4){ $fd->[0][($i)-1] = 2 }
When you want to do complicated things, the full power of Perl is at your disposal. You're not really afraid of sigils, are you?
AUTHOR
Thomas Orgis <thomas@orgis.org>
COPYRIGHT AND LICENSE
Copyright (C) 2004-2012, Thomas Orgis.
This module is free software; you can redistribute it and/or modify it under the same terms as Perl 5.10.0. For more details, see the full text of the licenses in the directory LICENSES.
This program is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose.