Revert "input checks in math and new function definitions"

This reverts commit 19e5a9504a5c1cb9cb58753e4633f263ad8b0014.
This commit is contained in:
RonaldoCMP 2020-07-29 21:50:22 +01:00
parent 19e5a9504a
commit babc10d60d
4 changed files with 229 additions and 462 deletions

View File

@ -99,14 +99,6 @@ function is_finite(v) = is_num(0*v);
function is_range(x) = !is_list(x) && is_finite(x[0]+x[1]+x[2]) ; function is_range(x) = !is_list(x) && is_finite(x[0]+x[1]+x[2]) ;
// Function: valid_range()
// Description:
// Returns true if its argument is a valid range (deprecated range is excluded).
function valid_range(ind) =
is_range(ind)
&& ( ( ind[1]>0 && ind[0]<=ind[2]) || (ind[1]<0 && ind[0]>=ind[2]) );
// Function: is_list_of() // Function: is_list_of()
// Usage: // Usage:
// is_list_of(list, pattern) // is_list_of(list, pattern)
@ -141,15 +133,10 @@ function is_list_of(list,pattern) =
// is_consistent([[3,[3,4,[5]]], [5,[2,9,[9]]]]); // Returns true // is_consistent([[3,[3,4,[5]]], [5,[2,9,[9]]]]); // Returns true
// is_consistent([[3,[3,4,[5]]], [5,[2,9,9]]]); // Returns false // is_consistent([[3,[3,4,[5]]], [5,[2,9,9]]]); // Returns false
function is_consistent(list) = function is_consistent(list) =
is_list_of(list, _list_pattern(list[0])); is_list(list) && is_list_of(list, list[0]);
//Internal function
//Creates a list with the same structure of `list` with each of its elements substituted by 0.
// `list` must be a list
function _list_pattern(list) =
is_list(list)
? [for(entry=list) is_list(entry) ? _list_pattern(entry) : 0]
: 0;
// Function: same_shape() // Function: same_shape()
// Usage: // Usage:
@ -159,7 +146,7 @@ function _list_pattern(list) =
// Example: // Example:
// same_shape([3,[4,5]],[7,[3,4]]); // Returns true // same_shape([3,[4,5]],[7,[3,4]]); // Returns true
// same_shape([3,4,5], [7,[3,4]]); // Returns false // same_shape([3,4,5], [7,[3,4]]); // Returns false
function same_shape(a,b) = _list_pattern(a) == b*0; function same_shape(a,b) = a*0 == b*0;
// Section: Handling `undef`s. // Section: Handling `undef`s.

471
math.scad
View File

@ -33,10 +33,7 @@ NAN = acos(2); // The value `nan`, useful for comparisons.
// sqr([3,4]); // Returns: [9,16] // sqr([3,4]); // Returns: [9,16]
// sqr([[1,2],[3,4]]); // Returns [[1,4],[9,16]] // sqr([[1,2],[3,4]]); // Returns [[1,4],[9,16]]
// sqr([[1,2],3]); // Returns [[1,4],9] // sqr([[1,2],3]); // Returns [[1,4],9]
function sqr(x) = function sqr(x) = is_list(x) ? [for(val=x) sqr(val)] : x*x;
is_list(x) ? [for(val=x) sqr(val)] :
is_finite(x) ? x*x :
assert(is_finite(x) || is_vector(x), "Input is not neither a number nor a list of numbers.");
// Function: log2() // Function: log2()
@ -48,11 +45,8 @@ function sqr(x) =
// log2(0.125); // Returns: -3 // log2(0.125); // Returns: -3
// log2(16); // Returns: 4 // log2(16); // Returns: 4
// log2(256); // Returns: 8 // log2(256); // Returns: 8
function log2(x) = function log2(x) = ln(x)/ln(2);
assert( is_finite(x), "Input is not a number.")
ln(x)/ln(2);
// this may return NAN or INF; should it check x>0 ?
// Function: hypot() // Function: hypot()
// Usage: // Usage:
@ -66,9 +60,7 @@ function log2(x) =
// Example: // Example:
// l = hypot(3,4); // Returns: 5 // l = hypot(3,4); // Returns: 5
// l = hypot(3,4,5); // Returns: ~7.0710678119 // l = hypot(3,4,5); // Returns: ~7.0710678119
function hypot(x,y,z=0) = function hypot(x,y,z=0) = norm([x,y,z]);
assert( is_vector([x,y,z]), "Improper number(s).")
norm([x,y,z]);
// Function: factorial() // Function: factorial()
@ -84,53 +76,11 @@ function hypot(x,y,z=0) =
// y = factorial(6); // Returns: 720 // y = factorial(6); // Returns: 720
// z = factorial(9); // Returns: 362880 // z = factorial(9); // Returns: 362880
function factorial(n,d=0) = function factorial(n,d=0) =
assert(is_int(n) && is_int(d) && n>=0 && d>=0, "Factorial is not defined for negative numbers") assert(n>=0 && d>=0, "Factorial is not defined for negative numbers")
assert(d<=n, "d cannot be larger than n") assert(d<=n, "d cannot be larger than n")
product([1,for (i=[n:-1:d+1]) i]); product([1,for (i=[n:-1:d+1]) i]);
// Function: binomial()
// Usage:
// x = binomial(n);
// Description:
// Returns the binomial coefficients of the integer `n`.
// Arguments:
// n = The integer to get the binomial coefficients of
// Example:
// x = binomial(3); // Returns: [1,3,3,1]
// y = binomial(4); // Returns: [1,4,6,4,1]
// z = binomial(6); // Returns: [1,6,15,20,15,6,1]
function binomial(n) =
assert( is_int(n) && n>0, "Input is not an integer greater than 0.")
[for( c = 1, i = 0;
i<=n;
c = c*(n-i)/(i+1), i = i+1
) c ] ;
// Function: binomial_coefficient()
// Usage:
// x = binomial_coefficient(n,k);
// Description:
// Returns the k-th binomial coefficient of the integer `n`.
// Arguments:
// n = The integer to get the binomial coefficient of
// k = The binomial coefficient index
// Example:
// x = binomial_coefficient(3,2); // Returns: 3
// y = binomial_coefficient(10,6); // Returns: 210
function binomial_coefficient(n,k) =
assert( is_int(n) && is_int(k), "Some input is not a number.")
k < 0 || k > n ? 0 :
k ==0 || k ==n ? 1 :
let( k = min(k, n-k),
b = [for( c = 1, i = 0;
i<=k;
c = c*(n-i)/(i+1), i = i+1
) c] )
b[len(b)-1];
// Function: lerp() // Function: lerp()
// Usage: // Usage:
// x = lerp(a, b, u); // x = lerp(a, b, u);
@ -141,8 +91,8 @@ function binomial_coefficient(n,k) =
// If `u` is 0.0, then the value of `a` is returned. // If `u` is 0.0, then the value of `a` is returned.
// If `u` is 1.0, then the value of `b` is returned. // If `u` is 1.0, then the value of `b` is returned.
// If `u` is a range, or list of numbers, returns a list of interpolated values. // If `u` is a range, or list of numbers, returns a list of interpolated values.
// It is valid to use a `u` value outside the range 0 to 1. The result will be an extrapolation // It is valid to use a `u` value outside the range 0 to 1. The result will be a predicted
// along the slope formed by `a` and `b`. // value along the slope formed by `a` and `b`, but not between those two values.
// Arguments: // Arguments:
// a = First value or vector. // a = First value or vector.
// b = Second value or vector. // b = Second value or vector.
@ -163,9 +113,9 @@ function binomial_coefficient(n,k) =
// rainbow(pts) translate($item) circle(d=3,$fn=8); // rainbow(pts) translate($item) circle(d=3,$fn=8);
function lerp(a,b,u) = function lerp(a,b,u) =
assert(same_shape(a,b), "Bad or inconsistent inputs to lerp") assert(same_shape(a,b), "Bad or inconsistent inputs to lerp")
is_finite(u)? (1-u)*a + u*b : is_num(u)? (1-u)*a + u*b :
assert(is_finite(u) || is_vector(u) || valid_range(u), "Input u to lerp must be a number, vector, or range.") assert(!is_undef(u)&&!is_bool(u)&&!is_string(u), "Input u to lerp must be a number, vector, or range.")
[for (v = u) (1-v)*a + v*b ]; [for (v = u) lerp(a,b,v)];
@ -174,45 +124,40 @@ function lerp(a,b,u) =
// Function: sinh() // Function: sinh()
// Description: Takes a value `x`, and returns the hyperbolic sine of it. // Description: Takes a value `x`, and returns the hyperbolic sine of it.
function sinh(x) = function sinh(x) =
assert(is_finite(x), "The input must be a finite number.")
(exp(x)-exp(-x))/2; (exp(x)-exp(-x))/2;
// Function: cosh() // Function: cosh()
// Description: Takes a value `x`, and returns the hyperbolic cosine of it. // Description: Takes a value `x`, and returns the hyperbolic cosine of it.
function cosh(x) = function cosh(x) =
assert(is_finite(x), "The input must be a finite number.")
(exp(x)+exp(-x))/2; (exp(x)+exp(-x))/2;
// Function: tanh() // Function: tanh()
// Description: Takes a value `x`, and returns the hyperbolic tangent of it. // Description: Takes a value `x`, and returns the hyperbolic tangent of it.
function tanh(x) = function tanh(x) =
assert(is_finite(x), "The input must be a finite number.")
sinh(x)/cosh(x); sinh(x)/cosh(x);
// Function: asinh() // Function: asinh()
// Description: Takes a value `x`, and returns the inverse hyperbolic sine of it. // Description: Takes a value `x`, and returns the inverse hyperbolic sine of it.
function asinh(x) = function asinh(x) =
assert(is_finite(x), "The input must be a finite number.")
ln(x+sqrt(x*x+1)); ln(x+sqrt(x*x+1));
// Function: acosh() // Function: acosh()
// Description: Takes a value `x`, and returns the inverse hyperbolic cosine of it. // Description: Takes a value `x`, and returns the inverse hyperbolic cosine of it.
function acosh(x) = function acosh(x) =
assert(is_finite(x), "The input must be a finite number.")
ln(x+sqrt(x*x-1)); ln(x+sqrt(x*x-1));
// Function: atanh() // Function: atanh()
// Description: Takes a value `x`, and returns the inverse hyperbolic tangent of it. // Description: Takes a value `x`, and returns the inverse hyperbolic tangent of it.
function atanh(x) = function atanh(x) =
assert(is_finite(x), "The input must be a finite number.")
ln((1+x)/(1-x))/2; ln((1+x)/(1-x))/2;
// Section: Quantization // Section: Quantization
// Function: quant() // Function: quant()
@ -240,10 +185,7 @@ function atanh(x) =
// quant([9,10,10.4,10.5,11,12],3); // Returns: [9,9,9,12,12,12] // quant([9,10,10.4,10.5,11,12],3); // Returns: [9,9,9,12,12,12]
// quant([[9,10,10.4],[10.5,11,12]],3); // Returns: [[9,9,9],[12,12,12]] // quant([[9,10,10.4],[10.5,11,12]],3); // Returns: [[9,9,9],[12,12,12]]
function quant(x,y) = function quant(x,y) =
assert(is_finite(y) && y>0, "The multiple must be a non zero integer.") is_list(x)? [for (v=x) quant(v,y)] :
is_list(x)
? [for (v=x) quant(v,y)]
: assert( is_finite(x), "The input to quantize must be a number or a list of numbers.")
floor(x/y+0.5)*y; floor(x/y+0.5)*y;
@ -272,10 +214,7 @@ function quant(x,y) =
// quantdn([9,10,10.4,10.5,11,12],3); // Returns: [9,9,9,9,9,12] // quantdn([9,10,10.4,10.5,11,12],3); // Returns: [9,9,9,9,9,12]
// quantdn([[9,10,10.4],[10.5,11,12]],3); // Returns: [[9,9,9],[9,9,12]] // quantdn([[9,10,10.4],[10.5,11,12]],3); // Returns: [[9,9,9],[9,9,12]]
function quantdn(x,y) = function quantdn(x,y) =
assert(is_finite(y) && !approx(y,0), "The multiple must be a non zero integer.") is_list(x)? [for (v=x) quantdn(v,y)] :
is_list(x)
? [for (v=x) quantdn(v,y)]
: assert( is_finite(x), "The input to quantize must be a number or a list of numbers.")
floor(x/y)*y; floor(x/y)*y;
@ -304,10 +243,7 @@ function quantdn(x,y) =
// quantup([9,10,10.4,10.5,11,12],3); // Returns: [9,12,12,12,12,12] // quantup([9,10,10.4,10.5,11,12],3); // Returns: [9,12,12,12,12,12]
// quantup([[9,10,10.4],[10.5,11,12]],3); // Returns: [[9,12,12],[12,12,12]] // quantup([[9,10,10.4],[10.5,11,12]],3); // Returns: [[9,12,12],[12,12,12]]
function quantup(x,y) = function quantup(x,y) =
assert(is_finite(y) && !approx(y,0), "The multiple must be a non zero integer.") is_list(x)? [for (v=x) quantup(v,y)] :
is_list(x)
? [for (v=x) quantup(v,y)]
: assert( is_finite(x), "The input to quantize must be a number or a list of numbers.")
ceil(x/y)*y; ceil(x/y)*y;
@ -328,9 +264,7 @@ function quantup(x,y) =
// constrain(0.3, -1, 1); // Returns: 0.3 // constrain(0.3, -1, 1); // Returns: 0.3
// constrain(9.1, 0, 9); // Returns: 9 // constrain(9.1, 0, 9); // Returns: 9
// constrain(-0.1, 0, 9); // Returns: 0 // constrain(-0.1, 0, 9); // Returns: 0
function constrain(v, minval, maxval) = function constrain(v, minval, maxval) = min(maxval, max(minval, v));
assert( is_finite(v+minval+maxval), "Input must be finite number(s).")
min(maxval, max(minval, v));
// Function: posmod() // Function: posmod()
@ -349,9 +283,7 @@ function constrain(v, minval, maxval) =
// posmod(270,360); // Returns: 270 // posmod(270,360); // Returns: 270
// posmod(700,360); // Returns: 340 // posmod(700,360); // Returns: 340
// posmod(3,2.5); // Returns: 0.5 // posmod(3,2.5); // Returns: 0.5
function posmod(x,m) = function posmod(x,m) = (x%m+m)%m;
assert( is_finite(x) && is_finite(m) && !approx(m,0) , "Input must be finite numbers. The divisor cannot be zero.")
(x%m+m)%m;
// Function: modang(x) // Function: modang(x)
@ -367,7 +299,6 @@ function posmod(x,m) =
// modang(270,360); // Returns: -90 // modang(270,360); // Returns: -90
// modang(700,360); // Returns: -20 // modang(700,360); // Returns: -20
function modang(x) = function modang(x) =
assert( is_finite(x), "Input must be a finite number.")
let(xx = posmod(x,360)) xx<180? xx : xx-360; let(xx = posmod(x,360)) xx<180? xx : xx-360;
@ -375,7 +306,7 @@ function modang(x) =
// Usage: // Usage:
// modrange(x, y, m, [step]) // modrange(x, y, m, [step])
// Description: // Description:
// Returns a normalized list of numbers from `x` to `y`, by `step`, modulo `m`. Wraps if `x` > `y`. // Returns a normalized list of values from `x` to `y`, by `step`, modulo `m`. Wraps if `x` > `y`.
// Arguments: // Arguments:
// x = The start value to constrain. // x = The start value to constrain.
// y = The end value to constrain. // y = The end value to constrain.
@ -387,7 +318,6 @@ function modang(x) =
// modrange(90,270,360, step=-45); // Returns: [90,45,0,315,270] // modrange(90,270,360, step=-45); // Returns: [90,45,0,315,270]
// modrange(270,90,360, step=-45); // Returns: [270,225,180,135,90] // modrange(270,90,360, step=-45); // Returns: [270,225,180,135,90]
function modrange(x, y, m, step=1) = function modrange(x, y, m, step=1) =
assert( is_finite(x+y+step+m) && !approx(m,0), "Input must be finite numbers. The module value cannot be zero.")
let( let(
a = posmod(x, m), a = posmod(x, m),
b = posmod(y, m), b = posmod(y, m),
@ -400,21 +330,20 @@ function modrange(x, y, m, step=1) =
// Function: rand_int() // Function: rand_int()
// Usage: // Usage:
// rand_int(minval,maxval,N,[seed]); // rand_int(min,max,N,[seed]);
// Description: // Description:
// Return a list of random integers in the range of minval to maxval, inclusive. // Return a list of random integers in the range of min to max, inclusive.
// Arguments: // Arguments:
// minval = Minimum integer value to return. // min = Minimum integer value to return.
// maxval = Maximum integer value to return. // max = Maximum integer value to return.
// N = Number of random integers to return. // N = Number of random integers to return.
// seed = If given, sets the random number seed. // seed = If given, sets the random number seed.
// Example: // Example:
// ints = rand_int(0,100,3); // ints = rand_int(0,100,3);
// int = rand_int(-10,10,1)[0]; // int = rand_int(-10,10,1)[0];
function rand_int(minval, maxval, N, seed=undef) = function rand_int(min, max, N, seed=undef) =
assert( is_finite(minval+maxval+N) && (is_undef(seed) || is_finite(seed) ), "Input must be finite numbers.") assert(max >= min, "Max value cannot be smaller than min")
assert(maxval >= minval, "Max value cannot be smaller than minval") let (rvect = is_def(seed) ? rands(min,max+1,N,seed) : rands(min,max+1,N))
let (rvect = is_def(seed) ? rands(minval,maxval+1,N,seed) : rands(minval,maxval+1,N))
[for(entry = rvect) floor(entry)]; [for(entry = rvect) floor(entry)];
@ -429,7 +358,6 @@ function rand_int(minval, maxval, N, seed=undef) =
// N = Number of random numbers to return. Default: 1 // N = Number of random numbers to return. Default: 1
// seed = If given, sets the random number seed. // seed = If given, sets the random number seed.
function gaussian_rands(mean, stddev, N=1, seed=undef) = function gaussian_rands(mean, stddev, N=1, seed=undef) =
assert( is_finite(mean+stddev+N) && (is_undef(seed) || is_finite(seed) ), "Input must be finite numbers.")
let(nums = is_undef(seed)? rands(0,1,N*2) : rands(0,1,N*2,seed)) let(nums = is_undef(seed)? rands(0,1,N*2) : rands(0,1,N*2,seed))
[for (i = list_range(N)) mean + stddev*sqrt(-2*ln(nums[i*2]))*cos(360*nums[i*2+1])]; [for (i = list_range(N)) mean + stddev*sqrt(-2*ln(nums[i*2]))*cos(360*nums[i*2+1])];
@ -446,10 +374,6 @@ function gaussian_rands(mean, stddev, N=1, seed=undef) =
// N = Number of random numbers to return. Default: 1 // N = Number of random numbers to return. Default: 1
// seed = If given, sets the random number seed. // seed = If given, sets the random number seed.
function log_rands(minval, maxval, factor, N=1, seed=undef) = function log_rands(minval, maxval, factor, N=1, seed=undef) =
assert( is_finite(minval+maxval+N)
&& (is_undef(seed) || is_finite(seed) )
&& factor>0,
"Input must be finite numbers. `factor` should be greater than zero.")
assert(maxval >= minval, "maxval cannot be smaller than minval") assert(maxval >= minval, "maxval cannot be smaller than minval")
let( let(
minv = 1-1/pow(factor,minval), minv = 1-1/pow(factor,minval),
@ -471,18 +395,18 @@ function gcd(a,b) =
b==0 ? abs(a) : gcd(b,a % b); b==0 ? abs(a) : gcd(b,a % b);
// Computes lcm for two integers // Computes lcm for two scalars
function _lcm(a,b) = function _lcm(a,b) =
assert(is_int(a) && is_int(b), "Invalid non-integer parameters to lcm") assert(is_int(a), "Invalid non-integer parameters to lcm")
assert(a!=0 && b!=0, "Arguments to lcm must be non zero") assert(is_int(b), "Invalid non-integer parameters to lcm")
assert(a!=0 && b!=0, "Arguments to lcm must be nonzero")
abs(a*b) / gcd(a,b); abs(a*b) / gcd(a,b);
// Computes lcm for a list of values // Computes lcm for a list of values
function _lcmlist(a) = function _lcmlist(a) =
len(a)==1 len(a)==1 ? a[0] :
? a[0] _lcmlist(concat(slice(a,0,len(a)-2),[lcm(a[len(a)-2],a[len(a)-1])]));
: _lcmlist(concat(slice(a,0,len(a)-2),[lcm(a[len(a)-2],a[len(a)-1])]));
// Function: lcm() // Function: lcm()
@ -494,10 +418,11 @@ function _lcmlist(a) =
// be non-zero integers. The output is always a positive integer. It is an error to pass zero // be non-zero integers. The output is always a positive integer. It is an error to pass zero
// as an argument. // as an argument.
function lcm(a,b=[]) = function lcm(a,b=[]) =
!is_list(a) && !is_list(b) !is_list(a) && !is_list(b) ? _lcm(a,b) :
? _lcm(a,b) let(
: let( arglist = concat(force_list(a),force_list(b)) ) arglist = concat(force_list(a),force_list(b))
assert(len(arglist)>0, "Invalid call to lcm with empty list(s)") )
assert(len(arglist)>0,"invalid call to lcm with empty list(s)")
_lcmlist(arglist); _lcmlist(arglist);
@ -506,9 +431,8 @@ function lcm(a,b=[]) =
// Function: sum() // Function: sum()
// Description: // Description:
// Returns the sum of all entries in the given consistent list. // Returns the sum of all entries in the given list.
// If passed an array of vectors, returns the sum the vectors. // If passed an array of vectors, returns a vector of sums of each part.
// If passed an array of matrices, returns the sum of the matrices.
// If passed an empty list, the value of `dflt` will be returned. // If passed an empty list, the value of `dflt` will be returned.
// Arguments: // Arguments:
// v = The list to get the sum of. // v = The list to get the sum of.
@ -517,9 +441,10 @@ function lcm(a,b=[]) =
// sum([1,2,3]); // returns 6. // sum([1,2,3]); // returns 6.
// sum([[1,2,3], [3,4,5], [5,6,7]]); // returns [9, 12, 15] // sum([[1,2,3], [3,4,5], [5,6,7]]); // returns [9, 12, 15]
function sum(v, dflt=0) = function sum(v, dflt=0) =
is_list(v) && len(v) == 0 ? dflt : is_vector(v) ? [for(i=v) 1]*v :
is_vector(v) || is_matrix(v)? [for(i=v) 1]*v :
assert(is_consistent(v), "Input to sum is non-numeric or inconsistent") assert(is_consistent(v), "Input to sum is non-numeric or inconsistent")
is_vector(v[0]) ? [for(i=v) 1]*v :
len(v) == 0 ? dflt :
_sum(v,v[0]*0); _sum(v,v[0]*0);
function _sum(v,_total,_i=0) = _i>=len(v) ? _total : _sum(v,_total+v[_i], _i+1); function _sum(v,_total,_i=0) = _i>=len(v) ? _total : _sum(v,_total+v[_i], _i+1);
@ -570,11 +495,10 @@ function sum_of_squares(v) = sum(vmul(v,v));
// Examples: // Examples:
// v = sum_of_sines(30, [[10,3,0], [5,5.5,60]]); // v = sum_of_sines(30, [[10,3,0], [5,5.5,60]]);
function sum_of_sines(a, sines) = function sum_of_sines(a, sines) =
assert( is_finite(a) && is_matrix(sines,undef,3), "Invalid input.") sum([
sum([ for (s = sines) for (s = sines) let(
let(
ss=point3d(s), ss=point3d(s),
v=ss[0]*sin(a*ss[1]+ss[2]) v=ss.x*sin(a*ss.y+ss.z)
) v ) v
]); ]);
@ -582,39 +506,26 @@ function sum_of_sines(a, sines) =
// Function: deltas() // Function: deltas()
// Description: // Description:
// Returns a list with the deltas of adjacent entries in the given list. // Returns a list with the deltas of adjacent entries in the given list.
// The list should be a consistent list of numeric components (numbers, vectors, matrix, etc).
// Given [a,b,c,d], returns [b-a,c-b,d-c]. // Given [a,b,c,d], returns [b-a,c-b,d-c].
// Arguments: // Arguments:
// v = The list to get the deltas of. // v = The list to get the deltas of.
// Example: // Example:
// deltas([2,5,9,17]); // returns [3,4,8]. // deltas([2,5,9,17]); // returns [3,4,8].
// deltas([[1,2,3], [3,6,8], [4,8,11]]); // returns [[2,4,5], [1,2,3]] // deltas([[1,2,3], [3,6,8], [4,8,11]]); // returns [[2,4,5], [1,2,3]]
function deltas(v) = function deltas(v) = [for (p=pair(v)) p.y-p.x];
assert( is_consistent(v) && len(v)>1 , "Inconsistent list or with length<=1.")
[for (p=pair(v)) p[1]-p[0]] ;
// Function: product() // Function: product()
// Description: // Description:
// Returns the product of all entries in the given list. // Returns the product of all entries in the given list.
// If passed a list of vectors of same dimension, returns a vector of products of each part. // If passed an array of vectors, returns a vector of products of each part.
// If passed a list of square matrices, returns a the resulting product matrix. // If passed an array of matrices, returns a the resulting product matrix.
// Arguments: // Arguments:
// v = The list to get the product of. // v = The list to get the product of.
// Example: // Example:
// product([2,3,4]); // returns 24. // product([2,3,4]); // returns 24.
// product([[1,2,3], [3,4,5], [5,6,7]]); // returns [15, 48, 105] // product([[1,2,3], [3,4,5], [5,6,7]]); // returns [15, 48, 105]
function product(v) = function product(v, i=0, tot=undef) = i>=len(v)? tot : product(v, i+1, ((tot==undef)? v[i] : is_vector(v[i])? vmul(tot,v[i]) : tot*v[i]));
assert( is_vector(v) || is_matrix(v) || ( is_matrix(v[0],square=true) && is_consistent(v)),
"Invalid input.")
_product(v, 1, v[0]);
function _product(v, i=0, _tot) =
i>=len(v) ? _tot :
_product( v,
i+1,
( is_vector(v[i])? vmul(_tot,v[i]) : _tot*v[i] ) );
// Function: outer_product() // Function: outer_product()
@ -623,22 +534,21 @@ function _product(v, i=0, _tot) =
// Usage: // Usage:
// M = outer_product(u,v); // M = outer_product(u,v);
function outer_product(u,v) = function outer_product(u,v) =
assert(is_vector(u) && is_vector(v), "The inputs must be vectors.") assert(is_vector(u) && is_vector(v))
[for(ui=u) ui*v]; assert(len(u)==len(v))
[for(i=[0:len(u)-1]) [for(j=[0:len(u)-1]) u[i]*v[j]]];
// Function: mean() // Function: mean()
// Description: // Description:
// Returns the arithmetic mean/average of all entries in the given array. // Returns the arithmatic mean/average of all entries in the given array.
// If passed a list of vectors, returns a vector of the mean of each part. // If passed a list of vectors, returns a vector of the mean of each part.
// Arguments: // Arguments:
// v = The list of values to get the mean of. // v = The list of values to get the mean of.
// Example: // Example:
// mean([2,3,4]); // returns 3. // mean([2,3,4]); // returns 3.
// mean([[1,2,3], [3,4,5], [5,6,7]]); // returns [3, 4, 5] // mean([[1,2,3], [3,4,5], [5,6,7]]); // returns [3, 4, 5]
function mean(v) = function mean(v) = sum(v)/len(v);
assert(is_list(v) && len(v)>0, "Invalid list.")
sum(v)/len(v);
// Function: median() // Function: median()
@ -646,33 +556,18 @@ function mean(v) =
// x = median(v); // x = median(v);
// Description: // Description:
// Given a list of numbers or vectors, finds the median value or midpoint. // Given a list of numbers or vectors, finds the median value or midpoint.
// If passed a list of vectors, returns the vector of the median of each component. // If passed a list of vectors, returns the vector of the median of each part.
function median(v) = function median(v) =
is_vector(v) ? (min(v)+max(v))/2 : assert(is_list(v))
is_matrix(v) ? [for(ti=transpose(v)) (min(ti)+max(ti))/2 ] assert(len(v)>0)
: assert(false , "Invalid input."); is_vector(v[0])? (
assert(is_consistent(v))
// Function: convolve() [
// Usage: for (i=idx(v[0]))
// x = convolve(p,q); let(vals = subindex(v,i))
// Description: (min(vals)+max(vals))/2
// Given two vectors, finds the convolution of them. ]
// The length of the returned vector is len(p)+len(q)-1 . ) : (min(v)+max(v))/2;
// Arguments:
// p = The first vector.
// q = The second vector.
// Example:
// a = convolve([1,1],[1,2,1]); // Returns: [1,3,3,1]
// b = convolve([1,2,3],[1,2,1])); // Returns: [1,4,8,8,3]
function convolve(p,q) =
p==[] || q==[] ? [] :
assert( is_vector(p) && is_vector(q), "The inputs should be vectors.")
let( n = len(p),
m = len(q))
[for(i=[0:n+m-2], k1 = max(0,i-n+1), k2 = min(i,m-1) )
[for(j=[k1:k2]) p[i-j] ] * [for(j=[k1:k2]) q[j] ]
];
// Section: Matrix math // Section: Matrix math
@ -687,7 +582,7 @@ function convolve(p,q) =
// want to solve Ax=b1 and Ax=b2 that you need to form the matrix transpose([b1,b2]) for the right hand side and then // want to solve Ax=b1 and Ax=b2 that you need to form the matrix transpose([b1,b2]) for the right hand side and then
// transpose the returned value. // transpose the returned value.
function linear_solve(A,b) = function linear_solve(A,b) =
assert(is_matrix(A), "Input should be a matrix.") assert(is_matrix(A))
let( let(
m = len(A), m = len(A),
n = len(A[0]) n = len(A[0])
@ -724,11 +619,7 @@ function matrix_inverse(A) =
// Description: // Description:
// Returns a submatrix with the specified index ranges or index sets. // Returns a submatrix with the specified index ranges or index sets.
function submatrix(M,ind1,ind2) = function submatrix(M,ind1,ind2) =
assert( is_matrix(M), "Input must be a matrix." ) [for(i=ind1) [for(j=ind2) M[i][j] ] ];
[for(i=ind1)
[for(j=ind2)
assert( ! is_undef(M[i][j]), "Invalid indexing." )
M[i][j] ] ];
// Function: qr_factor() // Function: qr_factor()
@ -737,7 +628,7 @@ function submatrix(M,ind1,ind2) =
// Calculates the QR factorization of the input matrix A and returns it as the list [Q,R]. This factorization can be // Calculates the QR factorization of the input matrix A and returns it as the list [Q,R]. This factorization can be
// used to solve linear systems of equations. // used to solve linear systems of equations.
function qr_factor(A) = function qr_factor(A) =
assert(is_matrix(A), "Input must be a matrix." ) assert(is_matrix(A))
let( let(
m = len(A), m = len(A),
n = len(A[0]) n = len(A[0])
@ -768,8 +659,8 @@ function _qr_factor(A,Q, column, m, n) =
// Function: back_substitute() // Function: back_substitute()
// Usage: back_substitute(R, b, [transpose]) // Usage: back_substitute(R, b, [transpose])
// Description: // Description:
// Solves the problem Rx=b where R is an upper triangular square matrix. The lower triangular entries of R are // Solves the problem Rx=b where R is an upper triangular square matrix. No check is made that the lower triangular entries
// ignored. If transpose==true then instead solve transpose(R)*x=b. // are actually zero. If transpose==true then instead solve transpose(R)*x=b.
// You can supply a compatible matrix b and it will produce the solution for every column of b. Note that if you want to // You can supply a compatible matrix b and it will produce the solution for every column of b. Note that if you want to
// solve Rx=b1 and Rx=b2 you must set b to transpose([b1,b2]) and then take the transpose of the result. If the matrix // solve Rx=b1 and Rx=b2 you must set b to transpose([b1,b2]) and then take the transpose of the result. If the matrix
// is singular (e.g. has a zero on the diagonal) then it returns []. // is singular (e.g. has a zero on the diagonal) then it returns [].
@ -803,9 +694,7 @@ function back_substitute(R, b, x=[],transpose = false) =
// Example: // Example:
// M = [ [6,-2], [1,8] ]; // M = [ [6,-2], [1,8] ];
// det = det2(M); // Returns: 50 // det = det2(M); // Returns: 50
function det2(M) = function det2(M) = M[0][0] * M[1][1] - M[0][1]*M[1][0];
assert( is_matrix(M,2,2), "Matrix should be 2x2." )
M[0][0] * M[1][1] - M[0][1]*M[1][0];
// Function: det3() // Function: det3()
@ -817,7 +706,6 @@ function det2(M) =
// M = [ [6,4,-2], [1,-2,8], [1,5,7] ]; // M = [ [6,4,-2], [1,-2,8], [1,5,7] ];
// det = det3(M); // Returns: -334 // det = det3(M); // Returns: -334
function det3(M) = function det3(M) =
assert( is_matrix(M,3,3), "Matrix should be 3x3." )
M[0][0] * (M[1][1]*M[2][2]-M[2][1]*M[1][2]) - M[0][0] * (M[1][1]*M[2][2]-M[2][1]*M[1][2]) -
M[1][0] * (M[0][1]*M[2][2]-M[2][1]*M[0][2]) + M[1][0] * (M[0][1]*M[2][2]-M[2][1]*M[0][2]) +
M[2][0] * (M[0][1]*M[1][2]-M[1][1]*M[0][2]); M[2][0] * (M[0][1]*M[1][2]-M[1][1]*M[0][2]);
@ -832,7 +720,7 @@ function det3(M) =
// M = [ [6,4,-2,9], [1,-2,8,3], [1,5,7,6], [4,2,5,1] ]; // M = [ [6,4,-2,9], [1,-2,8,3], [1,5,7,6], [4,2,5,1] ];
// det = determinant(M); // Returns: 2267 // det = determinant(M); // Returns: 2267
function determinant(M) = function determinant(M) =
assert(is_matrix(M,square=true), "Input should be a square matrix." ) assert(len(M)==len(M[0]))
len(M)==1? M[0][0] : len(M)==1? M[0][0] :
len(M)==2? det2(M) : len(M)==2? det2(M) :
len(M)==3? det3(M) : len(M)==3? det3(M) :
@ -865,11 +753,8 @@ function determinant(M) =
// n = optional width of matrix // n = optional width of matrix
// square = set to true to require a square matrix. Default: false // square = set to true to require a square matrix. Default: false
function is_matrix(A,m,n,square=false) = function is_matrix(A,m,n,square=false) =
is_list(A[0]) is_vector(A[0],n) && is_vector(A*(0*A[0]),m) &&
    && ( let(v = A*A[0]) is_num(0*(v*v)) ) // a matrix of finite numbers (!square || len(A)==len(A[0]));
    && (is_undef(n) || len(A[0])==n )
    && (is_undef(m) || len(A)==m )
    && ( !square || len(A)==len(A[0]));
// Section: Comparisons and Logic // Section: Comparisons and Logic
@ -892,10 +777,8 @@ function is_matrix(A,m,n,square=false) =
function approx(a,b,eps=EPSILON) = function approx(a,b,eps=EPSILON) =
a==b? true : a==b? true :
a*0!=b*0? false : a*0!=b*0? false :
is_list(a) is_list(a)? ([for (i=idx(a)) if(!approx(a[i],b[i],eps=eps)) 1] == []) :
? ([for (i=idx(a)) if( !approx(a[i],b[i],eps=eps)) 1] == []) (abs(a-b) <= eps);
: is_num(a) && is_num(b) && (abs(a-b) <= eps);
function _type_num(x) = function _type_num(x) =
@ -913,7 +796,7 @@ function _type_num(x) =
// Description: // Description:
// Compares two values. Lists are compared recursively. // Compares two values. Lists are compared recursively.
// Returns <0 if a<b. Returns >0 if a>b. Returns 0 if a==b. // Returns <0 if a<b. Returns >0 if a>b. Returns 0 if a==b.
// If types are not the same, then undef < bool < nan < num < str < list < range. // If types are not the same, then undef < bool < num < str < list < range.
// Arguments: // Arguments:
// a = First value to compare. // a = First value to compare.
// b = Second value to compare. // b = Second value to compare.
@ -937,14 +820,13 @@ function compare_vals(a, b) =
// a = First list to compare. // a = First list to compare.
// b = Second list to compare. // b = Second list to compare.
function compare_lists(a, b) = function compare_lists(a, b) =
a==b? 0 a==b? 0 : let(
: let( cmps = [
cmps = [ for(i=[0:1:min(len(a),len(b))-1]) for(i=[0:1:min(len(a),len(b))-1]) let(
let( cmp = compare_vals(a[i],b[i]) ) cmp = compare_vals(a[i],b[i])
if(cmp!=0) cmp ) if(cmp!=0) cmp
] ]
) ) cmps==[]? (len(a)-len(b)) : cmps[0];
cmps==[]? (len(a)-len(b)) : cmps[0];
// Function: any() // Function: any()
@ -961,13 +843,14 @@ function compare_lists(a, b) =
// any([[0,0], [1,0]]); // Returns true. // any([[0,0], [1,0]]); // Returns true.
function any(l, i=0, succ=false) = function any(l, i=0, succ=false) =
(i>=len(l) || succ)? succ : (i>=len(l) || succ)? succ :
any( l, any(
i+1, l, i=i+1, succ=(
succ = is_list(l[i]) ? any(l[i]) : !(!l[i]) is_list(l[i])? any(l[i]) :
!(!l[i])
)
); );
// Function: all() // Function: all()
// Description: // Description:
// Returns true if all items in list `l` evaluate as true. // Returns true if all items in list `l` evaluate as true.
@ -982,12 +865,13 @@ function any(l, i=0, succ=false) =
// all([[0,0], [1,0]]); // Returns false. // all([[0,0], [1,0]]); // Returns false.
// all([[1,1], [1,1]]); // Returns true. // all([[1,1], [1,1]]); // Returns true.
function all(l, i=0, fail=false) = function all(l, i=0, fail=false) =
(i>=len(l) || fail)? !fail : (i>=len(l) || fail)? (!fail) :
all( l, all(
i+1, l, i=i+1, fail=(
fail = is_list(l[i]) ? !all(l[i]) : !l[i] is_list(l[i])? !all(l[i]) :
) ; !l[i]
)
);
// Function: count_true() // Function: count_true()
@ -1020,21 +904,6 @@ function count_true(l, nmax=undef, i=0, cnt=0) =
); );
function count_true(l, nmax) =
!is_list(l) ? !(!l) ? 1: 0 :
let( c = [for( i = 0,
n = !is_list(l[i]) ? !(!l[i]) ? 1: 0 : undef,
c = !is_undef(n)? n : count_true(l[i], nmax),
s = c;
i<len(l) && (is_undef(nmax) || s<nmax);
i = i+1,
n = !is_list(l[i]) ? !(!l[i]) ? 1: 0 : undef,
c = !is_undef(n) || (i==len(l))? n : count_true(l[i], nmax-s),
s = s+c
) s ] )
len(c)<len(l)? nmax: c[len(c)-1];
// Section: Calculus // Section: Calculus
@ -1052,30 +921,21 @@ function count_true(l, nmax) =
// between data[i+1] and data[i], and the data values will be linearly resampled at each corner // between data[i+1] and data[i], and the data values will be linearly resampled at each corner
// to produce a uniform spacing for the derivative estimate. At the endpoints a single point method // to produce a uniform spacing for the derivative estimate. At the endpoints a single point method
// is used: f'(t) = (f(t+h)-f(t))/h. // is used: f'(t) = (f(t+h)-f(t))/h.
// Arguments:
// data = the list of the elements to compute the derivative of.
// h = the parametric sampling of the data.
// closed = boolean to indicate if the data set should be wrapped around from the end to the start.
function deriv(data, h=1, closed=false) = function deriv(data, h=1, closed=false) =
assert( is_consistent(data) , "Input list is not consistent or not numerical.")
assert( len(data)>=2, "Input `data` should have at least 2 elements.")
assert( is_finite(h) || is_vector(h), "The sampling `h` must be a number or a list of numbers." )
assert( is_num(h) || len(h) == len(data)-(closed?0:1),
str("Vector valued `h` must have length ",len(data)-(closed?0:1)))
is_vector(h) ? _deriv_nonuniform(data, h, closed=closed) : is_vector(h) ? _deriv_nonuniform(data, h, closed=closed) :
let( L = len(data) ) let( L = len(data) )
closed closed? [
? [
for(i=[0:1:L-1]) for(i=[0:1:L-1])
(data[(i+1)%L]-data[(L+i-1)%L])/2/h (data[(i+1)%L]-data[(L+i-1)%L])/2/h
] ] :
: let( let(
first = L<3 ? data[1]-data[0] : first =
L<3? data[1]-data[0] :
3*(data[1]-data[0]) - (data[2]-data[1]), 3*(data[1]-data[0]) - (data[2]-data[1]),
last = L<3 ? data[L-1]-data[L-2]: last =
L<3? data[L-1]-data[L-2]:
(data[L-3]-data[L-2])-3*(data[L-2]-data[L-1]) (data[L-3]-data[L-2])-3*(data[L-2]-data[L-1])
) ) [
[
first/2/h, first/2/h,
for(i=[1:1:L-2]) (data[i+1]-data[i-1])/2/h, for(i=[1:1:L-2]) (data[i+1]-data[i-1])/2/h,
last/2/h last/2/h
@ -1087,13 +947,15 @@ function _dnu_calc(f1,fc,f2,h1,h2) =
f1 = h2<h1 ? lerp(fc,f1,h2/h1) : f1 , f1 = h2<h1 ? lerp(fc,f1,h2/h1) : f1 ,
f2 = h1<h2 ? lerp(fc,f2,h1/h2) : f2 f2 = h1<h2 ? lerp(fc,f2,h1/h2) : f2
) )
(f2-f1) / 2 / min(h1,h2); (f2-f1) / 2 / min([h1,h2]);
function _deriv_nonuniform(data, h, closed) = function _deriv_nonuniform(data, h, closed) =
let( L = len(data) ) assert(len(h) == len(data)-(closed?0:1),str("Vector valued h must be length ",len(data)-(closed?0:1)))
closed let(
? [for(i=[0:1:L-1]) L = len(data)
)
closed? [for(i=[0:1:L-1])
_dnu_calc(data[(L+i-1)%L], data[i], data[(i+1)%L], select(h,i-1), h[i]) ] _dnu_calc(data[(L+i-1)%L], data[i], data[(i+1)%L], select(h,i-1), h[i]) ]
: [ : [
(data[1]-data[0])/h[0], (data[1]-data[0])/h[0],
@ -1105,23 +967,15 @@ function _deriv_nonuniform(data, h, closed) =
// Function: deriv2() // Function: deriv2()
// Usage: deriv2(data, [h], [closed]) // Usage: deriv2(data, [h], [closed])
// Description: // Description:
// Computes a numerical estimate of the second derivative of the data, which may be scalar or vector valued. // Computes a numerical esimate of the second derivative of the data, which may be scalar or vector valued.
// The `h` parameter gives the step size of your sampling so the derivative can be scaled correctly. // The `h` parameter gives the step size of your sampling so the derivative can be scaled correctly.
// If the `closed` parameter is true the data is assumed to be defined on a loop with data[0] adjacent to // If the `closed` parameter is true the data is assumed to be defined on a loop with data[0] adjacent to
// data[len(data)-1]. For internal points this function uses the approximation // data[len(data)-1]. For internal points this function uses the approximation
// f''(t) = (f(t-h)-2*f(t)+f(t+h))/h^2. For the endpoints (when closed=false), // f''(t) = (f(t-h)-2*f(t)+f(t+h))/h^2. For the endpoints (when closed=false) the algorithm
// when sufficient points are available, the method is either the four point expression // when sufficient points are available the method is either the four point expression
// f''(t) = (2*f(t) - 5*f(t+h) + 4*f(t+2*h) - f(t+3*h))/h^2 or // f''(t) = (2*f(t) - 5*f(t+h) + 4*f(t+2*h) - f(t+3*h))/h^2 or if five points are available
// f''(t) = (35*f(t) - 104*f(t+h) + 114*f(t+2*h) - 56*f(t+3*h) + 11*f(t+4*h)) / 12h^2 // f''(t) = (35*f(t) - 104*f(t+h) + 114*f(t+2*h) - 56*f(t+3*h) + 11*f(t+4*h)) / 12h^2
// if five points are available.
// Arguments:
// data = the list of the elements to compute the derivative of.
// h = the constant parametric sampling of the data.
// closed = boolean to indicate if the data set should be wrapped around from the end to the start.
function deriv2(data, h=1, closed=false) = function deriv2(data, h=1, closed=false) =
assert( is_consistent(data) , "Input list is not consistent or not numerical.")
assert( len(data)>=3, "Input list has less than 3 elements.")
assert( is_finite(h), "The sampling `h` must be a number." )
let( L = len(data) ) let( L = len(data) )
closed? [ closed? [
for(i=[0:1:L-1]) for(i=[0:1:L-1])
@ -1149,19 +1003,16 @@ function deriv2(data, h=1, closed=false) =
// Computes a numerical third derivative estimate of the data, which may be scalar or vector valued. // Computes a numerical third derivative estimate of the data, which may be scalar or vector valued.
// The `h` parameter gives the step size of your sampling so the derivative can be scaled correctly. // The `h` parameter gives the step size of your sampling so the derivative can be scaled correctly.
// If the `closed` parameter is true the data is assumed to be defined on a loop with data[0] adjacent to // If the `closed` parameter is true the data is assumed to be defined on a loop with data[0] adjacent to
// data[len(data)-1]. This function uses a five point derivative estimate, so the input data must include // data[len(data)-1]. This function uses a five point derivative estimate, so the input must include five points:
// at least five points:
// f'''(t) = (-f(t-2*h)+2*f(t-h)-2*f(t+h)+f(t+2*h)) / 2h^3. At the first and second points from the end // f'''(t) = (-f(t-2*h)+2*f(t-h)-2*f(t+h)+f(t+2*h)) / 2h^3. At the first and second points from the end
// the estimates are f'''(t) = (-5*f(t)+18*f(t+h)-24*f(t+2*h)+14*f(t+3*h)-3*f(t+4*h)) / 2h^3 and // the estimates are f'''(t) = (-5*f(t)+18*f(t+h)-24*f(t+2*h)+14*f(t+3*h)-3*f(t+4*h)) / 2h^3 and
// f'''(t) = (-3*f(t-h)+10*f(t)-12*f(t+h)+6*f(t+2*h)-f(t+3*h)) / 2h^3. // f'''(t) = (-3*f(t-h)+10*f(t)-12*f(t+h)+6*f(t+2*h)-f(t+3*h)) / 2h^3.
function deriv3(data, h=1, closed=false) = function deriv3(data, h=1, closed=false) =
assert( is_consistent(data) , "Input list is not consistent or not numerical.")
assert( len(data)>=5, "Input list has less than 5 elements.")
assert( is_finite(h), "The sampling `h` must be a number." )
let( let(
L = len(data), L = len(data),
h3 = h*h*h h3 = h*h*h
) )
assert(L>=5, "Need five points for 3rd derivative estimate")
closed? [ closed? [
for(i=[0:1:L-1]) for(i=[0:1:L-1])
(-data[(L+i-2)%L]+2*data[(L+i-1)%L]-2*data[(i+1)%L]+data[(i+2)%L])/2/h3 (-data[(L+i-2)%L]+2*data[(L+i-1)%L]-2*data[(i+1)%L]+data[(i+2)%L])/2/h3
@ -1185,22 +1036,16 @@ function deriv3(data, h=1, closed=false) =
// Function: C_times() // Function: C_times()
// Usage: C_times(z1,z2) // Usage: C_times(z1,z2)
// Description: // Description:
// Multiplies two complex numbers represented by 2D vectors. // Multiplies two complex numbers.
function C_times(z1,z2) = function C_times(z1,z2) = [z1.x*z2.x-z1.y*z2.y,z1.x*z2.y+z1.y*z2.x];
assert( is_vector(z1+z2,2), "Complex numbers should be represented by 2D vectors." )
[ z1.x*z2.x - z1.y*z2.y, z1.x*z2.y + z1.y*z2.x ];
// Function: C_div() // Function: C_div()
// Usage: C_div(z1,z2) // Usage: C_div(z1,z2)
// Description: // Description:
// Divides two complex numbers represented by 2D vectors. // Divides z1 by z2.
function C_div(z1,z2) = function C_div(z1,z2) = let(den = z2.x*z2.x + z2.y*z2.y)
assert( is_vector(z1,2) && is_vector(z2), "Complex numbers should be represented by 2D vectors." ) [(z1.x*z2.x + z1.y*z2.y)/den, (z1.y*z2.x-z1.x*z2.y)/den];
assert( !approx(z2,0), "The divisor `z2` cannot be zero." )
let(den = z2.x*z2.x + z2.y*z2.y)
[(z1.x*z2.x + z1.y*z2.y)/den, (z1.y*z2.x - z1.x*z2.y)/den];
// For the sake of consistence with Q_mul and vmul, C_times should be called C_mul
// Section: Polynomials // Section: Polynomials
@ -1211,48 +1056,25 @@ function C_div(z1,z2) =
// Evaluates specified real polynomial, p, at the complex or real input value, z. // Evaluates specified real polynomial, p, at the complex or real input value, z.
// The polynomial is specified as p=[a_n, a_{n-1},...,a_1,a_0] // The polynomial is specified as p=[a_n, a_{n-1},...,a_1,a_0]
// where a_n is the z^n coefficient. Polynomial coefficients are real. // where a_n is the z^n coefficient. Polynomial coefficients are real.
// The result is a number if `z` is a number and a complex number otherwise.
// Note: this should probably be recoded to use division by [1,-z], which is more accurate // Note: this should probably be recoded to use division by [1,-z], which is more accurate
// and avoids overflow with large coefficients, but requires poly_div to support complex coefficients. // and avoids overflow with large coefficients, but requires poly_div to support complex coefficients.
function polynomial(p, z, _k, _zk, _total) = function polynomial(p, z, k, zk, total) =
is_undef(_k) is_undef(k) ? polynomial(p, z, len(p)-1, is_num(z)? 1 : [1,0], is_num(z) ? 0 : [0,0]) :
? echo(poly=p) assert( is_vector(p), "Input polynomial coefficients must be a vector." ) k==-1 ? total :
let(p = _poly_trim(p)) polynomial(p, z, k-1, is_num(z) ? zk*z : C_times(zk,z), total+zk*p[k]);
assert( is_finite(z) || is_vector(z,2), "The value of `z` must be a real or a complex number." )
polynomial( p,
z,
len(p)-1,
is_num(z)? 1 : [1,0],
is_num(z) ? 0 : [0,0])
: _k==0
? _total + +_zk*p[0]
: polynomial( p,
z,
_k-1,
is_num(z) ? _zk*z : C_times(_zk,z),
_total+_zk*p[_k]);
function polynomial(p,z,k,total) =
     is_undef(k)
   ?    assert( is_vector(p) , "Input polynomial coefficients must be a vector." )
        assert( is_finite(z) || is_vector(z,2), "The value of `z` must be a real or a complex number." )
        polynomial( _poly_trim(p), z, 0, is_num(z) ? 0 : [0,0])
   : k==len(p) ? total
   : polynomial(p,z,k+1, is_num(z) ? total*z+p[k] : C_times(total,z)+[p[k],0]);
// Function: poly_mult() // Function: poly_mult()
// Usage // Usage
// polymult(p,q) // polymult(p,q)
// polymult([p1,p2,p3,...]) // polymult([p1,p2,p3,...])
// Description: // Descriptoin:
// Given a list of polynomials represented as real coefficient lists, with the highest degree coefficient first, // Given a list of polynomials represented as real coefficient lists, with the highest degree coefficient first,
// computes the coefficient list of the product polynomial. // computes the coefficient list of the product polynomial.
function poly_mult(p,q) = function poly_mult(p,q) =
is_undef(q) ? is_undef(q) ?
assert( is_list(p) assert(is_list(p) && (is_vector(p[0]) || p[0]==[]), "Invalid arguments to poly_mult")
&& []==[for(pi=p) if( !is_vector(pi) && pi!=[]) 0],
"Invalid arguments to poly_mult")
len(p)==2 ? poly_mult(p[0],p[1]) len(p)==2 ? poly_mult(p[0],p[1])
: poly_mult(p[0], poly_mult(select(p,1,-1))) : poly_mult(p[0], poly_mult(select(p,1,-1)))
: :
@ -1265,20 +1087,6 @@ function poly_mult(p,q) =
]) ])
]); ]);
function poly_mult(p,q) =
    is_undef(q) ?
       len(p)==2 ? poly_mult(p[0],p[1])
                 : poly_mult(p[0], poly_mult(select(p,1,-1)))
    :
    assert( is_vector(p) && is_vector(q),"Invalid arguments to poly_mult")
_poly_trim( [
                  for(n = [len(p)+len(q)-2:-1:0])
                      sum( [for(i=[0:1:len(p)-1])
                           let(j = len(p)+len(q)- 2 - n - i)
                           if (j>=0 && j<len(q)) p[i]*q[j]
                               ])
                   ]);
// Function: poly_div() // Function: poly_div()
// Usage: // Usage:
@ -1286,15 +1094,11 @@ function poly_mult(p,q) =
// Description: // Description:
// Computes division of the numerator polynomial by the denominator polynomial and returns // Computes division of the numerator polynomial by the denominator polynomial and returns
// a list of two polynomials, [quotient, remainder]. If the division has no remainder then // a list of two polynomials, [quotient, remainder]. If the division has no remainder then
// the zero polynomial [0] is returned for the remainder. Similarly if the quotient is zero // the zero polynomial [] is returned for the remainder. Similarly if the quotient is zero
// the returned quotient will be [0]. // the returned quotient will be [].
function poly_div(n,d,q) = function poly_div(n,d,q=[]) =
is_undef(q) assert(len(d)>0 && d[0]!=0 , "Denominator is zero or has leading zero coefficient")
? assert( is_vector(n) && is_vector(d) , "Invalid polynomials." ) len(n)<len(d) ? [q,_poly_trim(n)] :
let( d = _poly_trim(d) )
assert( d!=[0] , "Denominator cannot be a zero polynomial." )
poly_div(n,d,q=[])
: len(n)<len(d) ? [q==[]? [0]: q,_poly_trim(n)] :
let( let(
t = n[0] / d[0], t = n[0] / d[0],
newq = concat(q,[t]), newq = concat(q,[t]),
@ -1310,8 +1114,8 @@ function poly_div(n,d,q) =
// Removes leading zero terms of a polynomial. By default zeros must be exact, // Removes leading zero terms of a polynomial. By default zeros must be exact,
// or give epsilon for approximate zeros. // or give epsilon for approximate zeros.
function _poly_trim(p,eps=0) = function _poly_trim(p,eps=0) =
let( nz = [for(i=[0:1:len(p)-1]) if ( !approx(p[i],0,eps)) i]) let( nz = [for(i=[0:1:len(p)-1]) if (!approx(p[i],0,eps)) i])
len(nz)==0 ? [0] : select(p,nz[0],-1); len(nz)==0 ? [] : select(p,nz[0],-1);
// Function: poly_add() // Function: poly_add()
@ -1320,7 +1124,6 @@ function _poly_trim(p,eps=0) =
// Description: // Description:
// Computes the sum of two polynomials. // Computes the sum of two polynomials.
function poly_add(p,q) = function poly_add(p,q) =
assert( is_vector(p) && is_vector(q), "Invalid input polynomial(s)." )
let( plen = len(p), let( plen = len(p),
qlen = len(q), qlen = len(q),
long = plen>qlen ? p : q, long = plen>qlen ? p : q,
@ -1347,10 +1150,11 @@ function poly_add(p,q) =
// //
// Dario Bini. "Numerical computation of polynomial zeros by means of Aberth's Method", Numerical Algorithms, Feb 1996. // Dario Bini. "Numerical computation of polynomial zeros by means of Aberth's Method", Numerical Algorithms, Feb 1996.
// https://www.researchgate.net/publication/225654837_Numerical_computation_of_polynomial_zeros_by_means_of_Aberth's_method // https://www.researchgate.net/publication/225654837_Numerical_computation_of_polynomial_zeros_by_means_of_Aberth's_method
function poly_roots(p,tol=1e-14,error_bound=false) = function poly_roots(p,tol=1e-14,error_bound=false) =
assert( is_vector(p), "Invalid polynomial." ) assert(p!=[], "Input polynomial must have a nonzero coefficient")
let( p = _poly_trim(p,eps=0) ) assert(is_vector(p), "Input must be a vector")
assert( p!=[0], "Input polynomial cannot be zero." ) p[0] == 0 ? poly_roots(slice(p,1,-1),tol=tol,error_bound=error_bound) : // Strip leading zero coefficients
p[len(p)-1] == 0 ? // Strip trailing zero coefficients p[len(p)-1] == 0 ? // Strip trailing zero coefficients
let( solutions = poly_roots(select(p,0,-2),tol=tol, error_bound=error_bound)) let( solutions = poly_roots(select(p,0,-2),tol=tol, error_bound=error_bound))
(error_bound ? [ [[0,0], each solutions[0]], [0, each solutions[1]]] (error_bound ? [ [[0,0], each solutions[0]], [0, each solutions[1]]]
@ -1378,7 +1182,6 @@ function poly_roots(p,tol=1e-14,error_bound=false) =
) )
error_bound ? [roots, error] : roots; error_bound ? [roots, error] : roots;
// Internal function
// p = polynomial // p = polynomial
// pderiv = derivative polynomial of p // pderiv = derivative polynomial of p
// z = current guess for the roots // z = current guess for the roots
@ -1419,16 +1222,12 @@ function _poly_roots(p, pderiv, s, z, tol, i=0) =
// tol = tolerance for the complex polynomial root finder // tol = tolerance for the complex polynomial root finder
function real_roots(p,eps=undef,tol=1e-14) = function real_roots(p,eps=undef,tol=1e-14) =
assert( is_vector(p), "Invalid polynomial." )
let( p = _poly_trim(p,eps=0) )
assert( p!=[0], "Input polynomial cannot be zero." )
let( let(
roots_err = poly_roots(p,error_bound=true), roots_err = poly_roots(p,error_bound=true),
roots = roots_err[0], roots = roots_err[0],
err = roots_err[1] err = roots_err[1]
) )
is_def(eps) is_def(eps) ? [for(z=roots) if (abs(z.y)/(1+norm(z))<eps) z.x]
? [for(z=roots) if (abs(z.y)/(1+norm(z))<eps) z.x]
: [for(i=idx(roots)) if (abs(roots[i].y)<=err[i]) roots[i].x]; : [for(i=idx(roots)) if (abs(roots[i].y)<=err[i]) roots[i].x];
// vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap // vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap

View File

@ -169,6 +169,7 @@ module test_is_range() {
assert(!is_range(5)); assert(!is_range(5));
assert(!is_range(INF)); assert(!is_range(INF));
assert(!is_range(-INF)); assert(!is_range(-INF));
assert(!is_nan(NAN));
assert(!is_range("")); assert(!is_range(""));
assert(!is_range("foo")); assert(!is_range("foo"));
assert(!is_range([])); assert(!is_range([]));
@ -178,23 +179,7 @@ module test_is_range() {
assert(!is_range([3:4:"a"])); assert(!is_range([3:4:"a"]));
assert(is_range([3:1:5])); assert(is_range([3:1:5]));
} }
test_is_range(); test_is_nan();
module test_valid_range() {
assert(valid_range([0:0]));
assert(valid_range([0:1:0]));
assert(valid_range([0:1:10]));
assert(valid_range([0.1:1.1:2.1]));
assert(valid_range([0:-1:0]));
assert(valid_range([10:-1:0]));
assert(valid_range([2.1:-1.1:0.1]));
assert(!valid_range([10:1:0]));
assert(!valid_range([2.1:1.1:0.1]));
assert(!valid_range([0:-1:10]));
assert(!valid_range([0.1:-1.1:2.1]));
}
test_valid_range();
module test_is_list_of() { module test_is_list_of() {
@ -207,14 +192,10 @@ module test_is_list_of() {
} }
test_is_list_of(); test_is_list_of();
module test_is_consistent() { module test_is_consistent() {
assert(is_consistent([]));
assert(is_consistent([[],[]]));
assert(is_consistent([3,4,5])); assert(is_consistent([3,4,5]));
assert(is_consistent([[3,4],[4,5],[6,7]])); assert(is_consistent([[3,4],[4,5],[6,7]]));
assert(is_consistent([[[3],4],[[4],5]]));
assert(!is_consistent(5));
assert(!is_consistent(undef));
assert(!is_consistent([[3,4,5],[3,4]])); assert(!is_consistent([[3,4,5],[3,4]]));
assert(is_consistent([[3,[3,4,[5]]], [5,[2,9,[9]]]])); assert(is_consistent([[3,[3,4,[5]]], [5,[2,9,[9]]]]));
assert(!is_consistent([[3,[3,4,[5]]], [5,[2,9,9]]])); assert(!is_consistent([[3,[3,4,[5]]], [5,[2,9,9]]]));

View File

@ -782,8 +782,8 @@ test_deriv3();
module test_polynomial(){ module test_polynomial(){
assert_equal(polynomial([0],12),0); assert_equal(polynomial([],12),0);
assert_equal(polynomial([0],[12,4]),[0,0]); assert_equal(polynomial([],[12,4]),[0,0]);
assert_equal(polynomial([1,2,3,4],3),58); assert_equal(polynomial([1,2,3,4],3),58);
assert_equal(polynomial([1,2,3,4],[3,-1]),[47,-41]); assert_equal(polynomial([1,2,3,4],[3,-1]),[47,-41]);
assert_equal(polynomial([0,0,2],4),2); assert_equal(polynomial([0,0,2],4),2);
@ -879,17 +879,17 @@ test_qr_factor();
module test_poly_mult(){ module test_poly_mult(){
assert_equal(poly_mult([3,2,1],[4,5,6,7]),[12,23,32,38,20,7]); assert_equal(poly_mult([3,2,1],[4,5,6,7]),[12,23,32,38,20,7]);
assert_equal(poly_mult([3,2,1],[0]),[0]); assert_equal(poly_mult([3,2,1],[]),[]);
assert_equal(poly_mult([[1,2],[3,4],[5,6]]), [15,68,100,48]); assert_equal(poly_mult([[1,2],[3,4],[5,6]]), [15,68,100,48]);
assert_equal(poly_mult([[1,2],[0],[5,6]]), [0]); assert_equal(poly_mult([[1,2],[],[5,6]]), []);
assert_equal(poly_mult([[3,4,5],[0,0,0]]),[0]); assert_equal(poly_mult([[3,4,5],[0,0,0]]),[]);
} }
test_poly_mult(); test_poly_mult();
module test_poly_div(){ module test_poly_div(){
assert_equal(poly_div(poly_mult([4,3,3,2],[2,1,3]), [2,1,3]),[[4,3,3,2],[0]]); assert_equal(poly_div(poly_mult([4,3,3,2],[2,1,3]), [2,1,3]),[[4,3,3,2],[]]);
assert_equal(poly_div([1,2,3,4],[1,2,3,4,5]), [[0], [1,2,3,4]]); assert_equal(poly_div([1,2,3,4],[1,2,3,4,5]), [[], [1,2,3,4]]);
assert_equal(poly_div(poly_add(poly_mult([1,2,3,4],[2,0,2]), [1,1,2]), [1,2,3,4]), [[2,0,2],[1,1,2]]); assert_equal(poly_div(poly_add(poly_mult([1,2,3,4],[2,0,2]), [1,1,2]), [1,2,3,4]), [[2,0,2],[1,1,2]]);
assert_equal(poly_div([1,2,3,4], [1,-3]), [[1,5,18],[58]]); assert_equal(poly_div([1,2,3,4], [1,-3]), [[1,5,18],[58]]);
} }
@ -899,7 +899,7 @@ test_poly_div();
module test_poly_add(){ module test_poly_add(){
assert_equal(poly_add([2,3,4],[3,4,5,6]),[3,6,8,10]); assert_equal(poly_add([2,3,4],[3,4,5,6]),[3,6,8,10]);
assert_equal(poly_add([1,2,3,4],[-1,-2,3,4]), [6,8]); assert_equal(poly_add([1,2,3,4],[-1,-2,3,4]), [6,8]);
assert_equal(poly_add([1,2,3],-[1,2,3]),[0]); assert_equal(poly_add([1,2,3],-[1,2,3]),[]);
} }
test_poly_add(); test_poly_add();