Cleaned up code formatting in math.scad.

This commit is contained in:
Revar Desmera 2020-03-01 15:42:12 -08:00
parent f250f30044
commit 28114b49b5
2 changed files with 118 additions and 109 deletions

View File

@ -377,9 +377,6 @@ function log_rands(minval, maxval, factor, N=1, seed=undef) =
// Section: GCD/GCF, LCM
// If argument is a list return it. Otherwise return a singleton list containing the argument.
function _force_list(x) = is_list(x) ? x : [x];
// Function: gcd()
// Usage:
// gcd(a,b)
@ -415,7 +412,7 @@ function _lcmlist(a) =
function lcm(a,b=[]) =
!is_list(a) && !is_list(b) ? _lcm(a,b) :
let(
arglist = concat(_force_list(a),_force_list(b))
arglist = concat((is_list(a)?a:[a]), (is_list(b)?b:[b]))
)
assert(len(arglist)>0,"invalid call to lcm with empty list(s)")
_lcmlist(arglist);
@ -561,12 +558,7 @@ function linear_solve(A,b) =
// Usage: submatrix(M, ind1, ind2)
// Description:
// Returns a submatrix with the specified index ranges or index sets.
function submatrix(M,ind1,ind2) =
[for(i=ind1)
[for(j=ind2)
M[i][j]
]
];
function submatrix(M,ind1,ind2) = [for(i=ind1) [for(j=ind2) M[i][j] ] ];
// Function: qr_factor()
@ -583,9 +575,13 @@ function qr_factor(A) =
assert(len(dim)==2)
let(
qr =_qr_factor(A, column=0, m = m, n=m, Q=ident(m)),
Rzero = [for(i=[0:m-1]) [for(j=[0:n-1]) i>j ? 0 : qr[1][i][j]]]
)
[qr[0],Rzero];
Rzero = [
for(i=[0:m-1]) [
for(j=[0:n-1])
i>j ? 0 : qr[1][i][j]
]
]
) [qr[0],Rzero];
function _qr_factor(A,Q, column, m, n) =
column >= min(m-1,n) ? [Q,A] :
@ -608,17 +604,18 @@ function _qr_factor(A,Q, column, m, n) =
// are actually zero. If transpose==true then instead solve transpose(R)*x=b.
function back_substitute(R, b, x=[],transpose = false) =
let(n=len(b))
transpose ?
transpose?
reverse(back_substitute(
[for(i=[0:n-1]) [for(j=[0:n-1]) R[n-1-j][n-1-i]]],
reverse(b), x, false)) :
reverse(b), x, false
)) :
len(x) == n ? x :
let(
ind = n - len(x) - 1,
newvalue = len(x)==0 ? b[ind]/R[ind][ind] :
newvalue =
len(x)==0? b[ind]/R[ind][ind] :
(b[ind]-select(R[ind],ind+1,-1) * x)/R[ind][ind]
)
back_substitute(R, b, concat([newvalue],x));
) back_substitute(R, b, concat([newvalue],x));
// Function: det2()
@ -814,6 +811,8 @@ function count_true(l, nmax=undef, i=0, cnt=0) =
)
);
// Section: Calculus
// Function: deriv()
@ -827,18 +826,22 @@ function count_true(l, nmax=undef, i=0, cnt=0) =
// uses a two point method if sufficient points are available: f'(t) = (3*(f(t+h)-f(t)) - (f(t+2*h)-f(t+h)))/2h.
function deriv(data, h=1, closed=false) =
let( L = len(data) )
closed ?
[ for(i=[0:1:L-1]) (data[(i+1)%L]-data[(L+i-1)%L])/2/h ] :
let( first = L<3 ?
data[1]-data[0] :
closed? [
for(i=[0:1:L-1])
(data[(i+1)%L]-data[(L+i-1)%L])/2/h
] :
let(
first =
L<3? data[1]-data[0] :
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])
)
[ first/2/h,
) [
first/2/h,
for(i=[1:1:L-2]) (data[i+1]-data[i-1])/2/h,
last/2/h];
last/2/h
];
// Function: deriv2()
@ -854,21 +857,24 @@ function deriv(data, h=1, closed=false) =
// 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
function deriv2(data, h=1, closed=false) =
let( L = len(data) )
closed ?
[ for(i=[0:1:L-1]) (data[(i+1)%L]-2*data[i]+data[(L+i-1)%L])/h/h ] :
let( first = L<3 ? undef :
L==3 ? data[0] - 2*data[1] + data[2] :
L==4 ? 2*data[0] - 5*data[1] + 4*data[2] - data[3] :
closed? [
for(i=[0:1:L-1])
(data[(i+1)%L]-2*data[i]+data[(L+i-1)%L])/h/h
] :
let(
first = L<3? undef :
L==3? data[0] - 2*data[1] + data[2] :
L==4? 2*data[0] - 5*data[1] + 4*data[2] - data[3] :
(35*data[0] - 104*data[1] + 114*data[2] - 56*data[3] + 11*data[4])/12,
last = L<3 ? undef :
L==3 ? data[L-1] - 2*data[L-2] + data[L-3] :
L==4 ? -2*data[L-1] + 5*data[L-2] - 4*data[L-3] + data[L-4] :
last = L<3? undef :
L==3? data[L-1] - 2*data[L-2] + data[L-3] :
L==4? -2*data[L-1] + 5*data[L-2] - 4*data[L-3] + data[L-4] :
(35*data[L-1] - 104*data[L-2] + 114*data[L-3] - 56*data[L-4] + 11*data[L-5])/12
)
[ first/h/h,
) [
first/h/h,
for(i=[1:1:L-2]) (data[i+1]-2*data[i]+data[i-1])/h/h,
last/h/h];
last/h/h
];
// Function: deriv3()
@ -882,19 +888,21 @@ function deriv2(data, h=1, closed=false) =
// 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.
function deriv3(data, h=1, closed=false) =
let( L = len(data),
let(
L = len(data),
h3 = h*h*h
)
assert(L>=5, "Need five points for 3rd derivative estimate")
closed ?
[ 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] :
closed? [
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
] :
let(
first=(-5*data[0]+18*data[1]-24*data[2]+14*data[3]-3*data[4])/2,
second=(-3*data[0]+10*data[1]-12*data[2]+6*data[3]-data[4])/2,
last=(5*data[L-1]-18*data[L-2]+24*data[L-3]-14*data[L-4]+3*data[L-5])/2,
prelast=(3*data[L-1]-10*data[L-2]+12*data[L-3]-6*data[L-4]+data[L-5])/2
)
[
) [
first/h3,
second/h3,
for(i=[2:1:L-3]) (-data[i-2]+2*data[i-1]-2*data[i+1]+data[i+2])/2/h3,
@ -903,4 +911,5 @@ function deriv3(data, h=1, closed=false) =
];
// vim: noexpandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap

View File

@ -8,7 +8,7 @@
//////////////////////////////////////////////////////////////////////
BOSL_VERSION = [2,0,140];
BOSL_VERSION = [2,0,141];
// Section: BOSL Library Version Functions