761 {
762 if (values == nullptr) {
763 int nz_index = 0;
764 for (int i = 0; i < num_of_points_; ++i) {
765 iRow[nz_index] = i;
766 jCol[nz_index] = i;
767 ++nz_index;
768 }
769
770 for (int i = 0; i < num_of_points_; ++i) {
771 iRow[nz_index] = i;
772 jCol[nz_index] = v_offset_ + i;
773 ++nz_index;
774 }
775
776 for (int i = 0; i < num_of_points_; ++i) {
777 iRow[nz_index] = v_offset_ + i;
778 jCol[nz_index] = v_offset_ + i;
779 ++nz_index;
780 }
781
782 for (int i = 0; i < num_of_points_; ++i) {
783 iRow[nz_index] = a_offset_ + i;
784 jCol[nz_index] = a_offset_ + i;
785 ++nz_index;
786 }
787
788 for (int i = 0; i + 1 < num_of_points_; ++i) {
789 iRow[nz_index] = a_offset_ + i;
790 jCol[nz_index] = a_offset_ + i + 1;
791 ++nz_index;
792 }
793
794 for (int i = 0; i < nz_index; ++i) {
795 int r = iRow[i];
796 int c = jCol[i];
797 hessian_mapper_[to_hash_key(r, c)] = i;
798 }
799
800 } else {
801 std::fill(values, values + nele_hess, 0.0);
802
803
804 for (int i = 0; i < num_of_points_; ++i) {
805 auto s_index = i;
806 auto v_index = v_offset_ + i;
807
808 auto s = x[s_index];
809 auto v = x[v_index];
810
811 auto kappa = curvature_curve_.
Evaluate(0, s);
812 auto kappa_dot = curvature_curve_.
Evaluate(1, s);
813 auto kappa_ddot = curvature_curve_.
Evaluate(2, s);
814
815 auto v2 = v * v;
816 auto v3 = v2 * v;
817 auto v4 = v3 * v;
818
819 auto h_s_s_obj =
820 2.0 * kappa_dot * kappa_dot * v4 + 2.0 * kappa * kappa_ddot * v4;
821 auto h_s_s_index = hessian_mapper_[to_hash_key(s_index, s_index)];
822 values[h_s_s_index] +=
823 h_s_s_obj * w_overall_centripetal_acc_ * obj_factor;
824
825 auto h_s_v_obj = 8.0 * kappa * kappa_dot * v3;
826 auto h_s_v_index = hessian_mapper_[to_hash_key(s_index, v_index)];
827 values[h_s_v_index] +=
828 h_s_v_obj * w_overall_centripetal_acc_ * obj_factor;
829
830 auto h_v_v_obj = 12.0 * kappa * kappa * v2;
831 auto h_v_v_index = hessian_mapper_[to_hash_key(v_index, v_index)];
832 values[h_v_v_index] +=
833 h_v_v_obj * w_overall_centripetal_acc_ * obj_factor;
834 }
835
836
837 for (int i = 0; i < num_of_points_; ++i) {
838 auto h_s_s_index = hessian_mapper_[to_hash_key(i, i)];
839 values[h_s_s_index] += 2.0 * w_ref_s_ * obj_factor;
840 }
841
842
843 if (use_v_bound_) {
844 int lambda_offset = 4 * (num_of_points_ - 1);
845 for (int i = 0; i < num_of_points_; ++i) {
846 auto s_index = i;
847
848 auto s = x[s_index];
849
850 auto v_bound_ddot = v_bound_func_.
Evaluate(2, s);
851
852 auto h_s_s_constr = -1.0 * v_bound_ddot;
853 auto h_s_s_index = hessian_mapper_[to_hash_key(s_index, s_index)];
854 values[h_s_s_index] += h_s_s_constr * lambda[lambda_offset + i];
855 }
856 }
857
858 for (int i = 0; i < num_of_points_; ++i) {
859 auto a_index = a_offset_ + i;
860 auto h_index = hessian_mapper_[to_hash_key(a_index, a_index)];
861 values[h_index] += 2.0 * w_overall_a_ * obj_factor;
862 }
863
864 auto c = 2.0 / delta_t_ / delta_t_ * w_overall_j_ * obj_factor;
865 for (int i = 0; i + 1 < num_of_points_; ++i) {
866 auto a0_index = a_offset_ + i;
867 auto a1_index = a_offset_ + i + 1;
868
869 auto h_a0_a0_index = hessian_mapper_[to_hash_key(a0_index, a0_index)];
870 values[h_a0_a0_index] += c;
871
872 auto h_a0_a1_index = hessian_mapper_[to_hash_key(a0_index, a1_index)];
873 values[h_a0_a1_index] += -c;
874
875 auto h_a1_a1_index = hessian_mapper_[to_hash_key(a1_index, a1_index)];
876 values[h_a1_a1_index] += c;
877 }
878
879 for (int i = 0; i < num_of_points_; ++i) {
880 auto v_index = i + v_offset_;
881 auto key = to_hash_key(v_index, v_index);
882 auto index = hessian_mapper_[key];
883 values[index] += 2.0 * w_ref_v_ * obj_factor;
884 }
885
886 if (has_end_state_target_) {
887
888 auto s_end_index = num_of_points_ - 1;
889 auto s_key = to_hash_key(s_end_index, s_end_index);
890
891 auto s_index = hessian_mapper_[s_key];
892
893 values[s_index] += 2.0 * w_target_s_ * obj_factor;
894
895
896 auto v_end_index = 2 * num_of_points_ - 1;
897 auto v_key = to_hash_key(v_end_index, v_end_index);
898
899 auto v_index = hessian_mapper_[v_key];
900
901 values[v_index] += 2.0 * w_target_v_ * obj_factor;
902
903
904 auto a_end_index = 3 * num_of_points_ - 1;
905 auto a_key = to_hash_key(a_end_index, a_end_index);
906
907 auto a_index = hessian_mapper_[a_key];
908
909 values[a_index] += 2.0 * w_target_a_ * obj_factor;
910 }
911 }
912 return true;
913}