Matrix BeamEndContact3D::ExpMap(Vector th) // this function computes the exp map of the given vector { double sf1; double sf2; double sf3; double theta; // vector norm Vector theta_vec(BEC3_NUM_DIM); // input vector Matrix sk_theta(BEC3_NUM_DIM,BEC3_NUM_DIM); // skew of vector Matrix theta_theta(BEC3_NUM_DIM,BEC3_NUM_DIM); // dyadic product of vector Matrix Q(BEC3_NUM_DIM,BEC3_NUM_DIM); // Exonential Map Vector // initialize theta variables Q.Zero(); sk_theta.Zero(); theta_theta.Zero(); theta_vec = th; theta = theta_vec.Norm(); sk_theta = GetSkew(theta_vec); int i, j; for (int i = 0; i<3; i++) { for (int j = 0; j<3; j++) { theta_theta(i,j) = theta_vec(i)*theta_vec(j); } } // determine local variables sf1 = cos(theta); if (theta > 5.0e-3) { sf2 = (sin(theta))/theta; } else { // small theta approximation sf2 = 1.0 - theta*theta/6.0 + pow(theta,4.0)/120.0; } if (theta > 0.1) { sf3 = (1.0-cos(theta))/(theta*theta); } else { // small theta approximation sf3 = 0.5 - theta*theta/24.0 + pow(theta, 4.0)/720.0 -pow(theta,6.0)/40320.0 + pow(theta, 8.0)/3628800.0; } // compute exponental map vector Q = sf1*mEye1 + sf2*sk_theta + sf3*theta_theta; return Q; }
int main(void) { const std::string kDataFileName = "../../microChipData.txt"; DataMapped micro_chip_data(kDataFileName); const int kNumFeatures = micro_chip_data.num_features(); arma::vec theta_vec = arma::randu<arma::vec>(kNumFeatures+1,1); theta_vec.zeros(kNumFeatures+1,1); arma::vec gradient_vec = arma::randu<arma::vec>(kNumFeatures+1,1); gradient_vec.zeros(kNumFeatures+1,1); const int kNumTrainEx = micro_chip_data.num_train_ex(); arma::vec predictions_vec = arma::randu<arma::vec>(kNumTrainEx,1); const double kLambda = 1.0; const int kNumIterations = 400; RegularizedLogisticRegression reg_log_reg(kNumIterations,theta_vec,\ gradient_vec,predictions_vec,kLambda); // Computes initial cost. const std::vector<double> kTheta(kNumFeatures+1,0.0); std::vector<double> grad(kNumFeatures+1,0.0); const double kInitCost = reg_log_reg.ComputeCost(kTheta,grad,micro_chip_data); printf("Cost at initial theta (zeros): %.6f\n",kInitCost); printf("\n"); printf("Program paused. Press enter to continue.\n"); std::cin.ignore(); // Uses MMA algorithm to solve for optimum weights and cost. nlopt::opt opt(nlopt::LD_MMA,kNumFeatures+1); WrapperStruct wrap_struct; wrap_struct.reg_log_reg = ®_log_reg; wrap_struct.data_mapped = µ_chip_data; opt.set_min_objective(ComputeCostWrapper,&wrap_struct); opt.set_xtol_rel(1e-4); std::vector<double> nlopt_theta(kNumFeatures+1,0.0); double min_cost = 0.0; nlopt::result nlopt_result = opt.optimize(nlopt_theta,min_cost); for(int feature_index=0; feature_index<(kNumFeatures+1); feature_index++) { theta_vec(feature_index) = nlopt_theta[feature_index]; } // Computes accuracy on training set. const int kReturnCode2 = reg_log_reg.LabelPrediction(micro_chip_data); const arma::vec trainingPredict = reg_log_reg.predictions(); const arma::vec trainingLabels = micro_chip_data.training_labels(); int num_train_match = 0; for(int example_index=0; example_index<kNumTrainEx; example_index++) { if (trainingPredict(example_index) == trainingLabels(example_index)) { num_train_match++; } } printf("Train Accuracy: %.6f\n",(100.0*num_train_match/kNumTrainEx)); return 0; }