NB. Matrix to find spline 2nd derivatives (natural splines): NB. (SpMD x) on vector x yeilds matrix B, where (B mm y) NB. gives the spline 2nd derivatives, i.e., D = x spline y NB. SpMD=: monad define k1=. 6% h1=. }: h=. DEL y k3=. 6% h3=. }. h h2=. +: h3+h1 h1=. 0, (}. h1) h3=. (}: h3),0 AI=. trivert h1;h2;h3 NB. invert (N-2)x(N-2) AI=. 0,(|: 0,(|: AI),0),0 NB. border with zeros k1=. 0,k1,0 k3=. 0,k3,0 k2=. - (k1 + k3) AI mm full_matrix k1;k2;k3 ) NB. Invert a tridiagonal matrix by direct substitution. NB. Rows are a_i*x_(i-1), b_i*x_i, c_i*x_(i+1) NB. a,b,c have same length, i.e., a[0]=0 and c[n]=0. NB. Usage: inverse(full nxn matrix)<-- tridgl a;b;c NB. trivert=: monad define 'a b c'=. y n1=. <: n=. <: nn=. #b x=. %{.b inv=. x (<0 0)}(=@i. nn) v=. (x*{.c) 0}v=. nn#0 j=. 0 while. n1>:j=.>:j do. b1=. j{b a1=. j{a v0=. (<:j){v x=. %b1-a1*v0 c1=. j{c v=. (x*c1)j}v inv1=. j{inv inv0=. (<:j){inv inv=. (x*inv1-a1*inv0)j}inv end. x=. %(n{b)-(n{a)*(n1{v) inv=. (x*(n{inv)-(n{a)*(n1{inv)) n}inv j=. n while. 0<:j=.<:j do. inv1=. j{inv v1=. j{v inv2=. (>:j){inv inv=. (inv1-v1*inv2)j}inv end. ) NB. Expand the three diagonal bands of a tridiagonal NB. matrix into the full matrix. NB. Rows are a_i, b_i, c_i NB. a,b,c have same length, i.e., a[0]=0 and c[n]=0. NB. Usage: full_matrix a;b;c --> (full nxn matrix) NB. full_matrix=: monad define 'a b c'=. y abc=. b* I=. =@i. (#b) NB. I is the identity matrix abc=. abc+ 1|."1 (a* I) abc+ _1|."1 (c* I) )