I am currently trying to make a general B spline basis plotter with meta post.
The definition of a basis function is on wikipedia under properties.
The knots of a b spline should have the pattern 0's a number of times equal to the order of the spline, ascending values for a while, last value repeated to match the order
So for example for a spline of order 3: [0,0,0,1,2,3,4,5,6,6,6] makes a knot vector.
To this effect I tried coding that exact formula and printed the knot vector I fabricate to make sure I did things properly:
The above is one of the order 2 splines, it should look like a liner rising and then decreasing function (a pyramid) but instead it looks like an order 1 basis. I tried changing the order, but all of my functions are returning the same pattern.
Like, for example an order 3 function: 
My code is as follows;
\documentclass[border=10cm]{standalone} \usepackage{luamplib} \mplibnumbersystem{double} \usepackage[margin=0.5cm]{geometry} \begin{document} {\centering \begin{mplibcode} u:=1cm; numeric knots[]; vardef make_knots(expr order, num)= p_num = num - 1; v = 0; for i=0 upto order-1: knots[i] := v; endfor; for i=0 upto p_num - order: v := v+1; knots[i + order] := v; endfor; for i=0 upto order-1: knots[i + p_num + 1] := v; endfor; for i=0 upto order + p_num - 1: tmp := round(knots[i]); label.top(textext("\huge$"& decimal(tmp) &"$"), (2*i*u,6*u)); endfor; enddef; vardef calculate_basis(expr t, i, order)= numeric ret; if order=1: if (t >= knots[i]) and (t < knots[i+1]): ret = 1; else: ret = 0; fi; else: ret = ((t-knots[i]) / (knots[i] + order - knots[i])) * calculate_basis(t, i, order-1) + ((knots[i + order + 1] - t) / (knots[i + order + 1] - knots[i+1])) * calculate_basis(t, i+1, order-1); fi; ret enddef; color darkred, darkyellow, darkgreen, lightblue, brown, pink, orange; darkred := (0.8,0.0,0.0); darkyellow := (1.0,0.8,0.0); darkgreen := (0.0,0.6,0.0); lightblue := (0.0,0.8,1.0); brown := (0.5, 0.1, 0.1); pink := (1, 0.0, 0.8); orange := (1, 0.4, 0.0); %ignore first parameter while denbugging vardef plot_basis(expr j, order, color)= res := 100; for i=0 upto res-1: fraction := (i/res) * 6; %multiply by biggest knot value valS := calculate_basis(fraction, 1, 3); save pointS; pair pointS; pointS := ((i) * 2 / res, valS) * u * 5; %scale plot to make it visible draw pointS withpen pencircle scaled 5bp withcolor color; endfor; enddef; % Start figure beginfig(0); color colors[]; colors[0] = darkred; colors[1] = darkyellow; colors[2] = darkgreen; colors[3] = lightblue; colors[4] = pink; colors[5] = brown; colors[6] = orange; make_knots(2, 8); plot_basis(2, 2, colors[0]); endfig; \end{mplibcode} \par} \end{document} 

