/** The matrices for each combination could be explicitly expanded instead of using matrix multiplication. */ void Matrix4x4::FromEuler(float x, float y, float z, order_t order) { float xrad = x / 180.0f * 3.1415927f; float sx = sinf(xrad); float cx = cosf(xrad); Matrix4x4 xMat(1.0, 0.0, 0.0, 0.0, 0.0, cx, -sx, 0.0, 0.0, sx, cx, 0.0, 0.0, 0.0, 0.0, 1.0); float yrad = y / 180.0f * 3.1415927f; float sy = sinf(yrad); float cy = cosf(yrad); Matrix4x4 yMat( cy, 0.0, sy, 0.0, 0.0, 1.0, 0.0, 0.0, -sy, 0.0, cy, 0.0, 0.0, 0.0, 0.0, 1.0); float zrad = z / 180.0f * 3.1415927f; float sz = sinf(zrad); float cz = cosf(zrad); Matrix4x4 zMat( cz, -sz, 0.0, 0.0, sz, cz, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0); switch(order) { case kXYZ: *this = zMat * (yMat * xMat); break; case kYXZ: *this = zMat * (xMat * yMat); break; case kYZX: *this = xMat * (zMat * yMat); break; case kXZY: *this = yMat * (zMat * xMat); break; case kZYX: *this = xMat * (yMat * zMat); break; case kZXY: *this = yMat * (xMat * zMat); break; default: *this = xMat * (yMat * zMat); break; } }
void perceptron::trainRegression(double a, double *x, double *y, int k, int max) { // Prepare y matrix Eigen::MatrixXd yMat(k, 1); for(int i = 0; i < k; i++) yMat(i, 0) = y[i]; // Prepare x matrix (unfold the provided array) Eigen::MatrixXd xMat(k, n + 1); for(int i = 0; i < k; i++) { xMat(i, 0) = 1.0; for(int j = 0; j < n; j++) { xMat(i, j + 1) = x[n * i + j]; } } Eigen::MatrixXd xMatT = xMat.transpose(); Eigen::MatrixXd wMat = ((xMatT * xMat).inverse() * xMatT) * yMat; // W = ((Xt * X)^-1 * Xt) * Y // Map back to w Eigen::Map<Eigen::MatrixXd>(w, n + 1, 1) = wMat; }