Posts from the “Math” Category

Matrix for 2D Polynomial Surface

Given the polynomial coefficients of a 4 * 4 polynomial surface,

f(x,y) = p00 + p10*x + p01*y + p20*x^2 + p11*x*y + p02*y^2 + p30*x^3 + p21*x^2*y + p12*x*y^2 + p03*y^3 + p40*x^4 + p31*x^3*y + p22*x^2*y^2 + p13*x*y^3 + p04*y^4

how to conveniently compute f(x, y) in a shader?

If we just do a multiplication of 2 vectors each consists of 15 elements, it’s easy to make mistakes when composing the vectors. So I did a practice to use matrix to represent the equation for a 2 * 2 polynomial:

f(x,y) = p00 + p10*x + p01*y + p20*x^2 + p11*x*y + p02*y^2 =
poly33

Much cleaner right? Now we can use a generic matrix to represent a n * n polynomial:

f(x, y) = YPX,

where P is a (n + 1) * (n + 1) matrix of the coefficients:

P = [p_00, p_10, p_20, ............., p_n0
     p_01, p_11, p_21, ......, p_(n-1)1, 0
     p_02, p_12, p_22, ..., p_(n-2)2, 0, 0
     ...
     p_0n, 0, 0, ......................, 0],

Y is a 1 * (n + 1) vector for y values:

Y = [1, y, y^2, ..., y^n],

X is a (n + 1) * 1 vector for x values:

X = [1, x, x^2, ..., x^n]^T.

Gram-Schmidt Orthogonalization

Gram-Schmidt Orthogonalization produces a an orthogonal basis for a set of linearly independent vectors. So, the idea of getting a vector’s corresponding vector in the orthogonal basis is substracting the vector’s projections onto the preceding basis vectors by the vector itself. Then whatever left is orthogonal to all the preceding basis vectors.

My question was, why that further substractions of the projections do not affect the orthogonalization with previous basis vectors??? After a while of thinking.. I realize that it’s because further substractions all happen in spaces orthogonal to previous basis vectors, so of course whatever linear operations of vectors on the orthogonal spaces do not affect the resulting vector’s orthogonalization with previous basis vectors..

 

Exponential Function

exp(x) = 1 + x + x^2 / 2! + x^3 / 3! + x^4 / 4! + … + x^n / n!

float Exp(float x) {

float result = 0.f;

for(int i = 0; i < n; ++i) {

float a = pow(x, i);

float b = i * (i-1) * (i-2) * … * 1;

result += a/b;

}

return result;

}