calcBasisFunctionDerivatives function

List<List<double>> calcBasisFunctionDerivatives(
  1. int span,
  2. num u,
  3. int p,
  4. int n,
  5. List<double> U,
)

Implementation

List<List<double>> calcBasisFunctionDerivatives(int span, num u, int p, int n, List<double> U ) {

	final List<double> zeroArr = [];
	for ( int i = 0; i <= p; ++ i ){
		zeroArr[ i ] = 0.0;
  }

	final List<List<double>> ders = [];

	for ( int i = 0; i <= n; ++ i ){
		ders[ i ] = zeroArr.sublist( 0 );
  }

	final ndu = [];

	for ( int i = 0; i <= p; ++ i ){
		ndu[ i ] = zeroArr.sublist( 0 );
  }

	ndu[ 0 ][ 0 ] = 1.0;

	final List<double> left = zeroArr.sublist( 0 );
	final List<double> right = zeroArr.sublist( 0 );

	for ( int j = 1; j <= p; ++ j ) {
		left[ j ] = u - U[ span + 1 - j ];
		right[ j ] = U[ span + j ] - u;

		double saved = 0.0;

		for ( int r = 0; r < j; ++ r ) {
			final rv = right[ r + 1 ];
			final lv = left[ j - r ];
			ndu[ j ][ r ] = rv + lv;

			final temp = ndu[ r ][ j - 1 ] / ndu[ j ][ r ];
			ndu[ r ][ j ] = saved + rv * temp;
			saved = lv * temp;
		}

		ndu[ j ][ j ] = saved;
	}

	for ( int j = 0; j <= p; ++ j ) {
		ders[ 0 ][ j ] = ndu[ j ][ p ];
	}

	for ( int r = 0; r <= p; ++ r ) {
		int s1 = 0;
		int s2 = 1;

		final a = [];
		for ( int i = 0; i <= p; ++ i ) {
			a[ i ] = zeroArr.sublist( 0 );
		}

		a[ 0 ][ 0 ] = 1.0;

		for ( int k = 1; k <= n; ++ k ) {
			double d = 0.0;
			final rk = r - k;
			final pk = p - k;

			if ( r >= k ) {
				a[ s2 ][ 0 ] = a[ s1 ][ 0 ] / ndu[ pk + 1 ][ rk ];
				d = a[ s2 ][ 0 ] * ndu[ rk ][ pk ];
			}

			final j1 = ( rk >= - 1 ) ? 1 : - rk;
			final j2 = ( r - 1 <= pk ) ? k - 1 : p - r;

			for ( int j = j1; j <= j2; ++ j ) {
				a[ s2 ][ j ] = ( a[ s1 ][ j ] - a[ s1 ][ j - 1 ] ) / ndu[ pk + 1 ][ rk + j ];
				d += a[ s2 ][ j ] * ndu[ rk + j ][ pk ];
			}

			if ( r <= pk ) {
				a[ s2 ][ k ] = - a[ s1 ][ k - 1 ] / ndu[ pk + 1 ][ r ];
				d += a[ s2 ][ k ] * ndu[ r ][ pk ];
			}

			ders[ k ][ r ] = d;

			final j = s1;
			s1 = s2;
			s2 = j;
		}
	}

	num r = p;

	for ( int k = 1; k <= n; ++ k ) {
		for ( int j = 0; j <= p; ++ j ) {
			ders[ k ][ j ] *= r;
		}
		r *= p - k;
	}

	return ders;
}