AdditionalIceUtils.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #include "optimization/AdditionalIceUtils.h"
  2. #include "vectort.h" // ICE
  3. #include "ludecomp.h" //ICE
  4. #include "mateigen.h" //ICE
  5. #include <cassert>
  6. using namespace opt;
  7. using namespace ice;
  8. matrix_type & MatrixAddVal ( matrix_type & mat, double val )
  9. {
  10. for(int i=0; i< mat.rows(); ++i)
  11. {
  12. for(int j =0; j<mat.cols(); ++j)
  13. {
  14. mat[i][j] += val;
  15. }
  16. }
  17. return mat;
  18. }
  19. double MatrixSum(const matrix_type &mat)
  20. {
  21. double sum=0.0;
  22. for(int i=0; i< mat.rows(); ++i)
  23. {
  24. for(int j =0; j<mat.cols(); ++j)
  25. {
  26. sum += mat[i][j];
  27. }
  28. }
  29. return sum;
  30. }
  31. void linSolve(const opt::matrix_type &A, opt::matrix_type &x, const opt::matrix_type b)
  32. {
  33. int n=A.cols();
  34. assert(x.rows() == n && b.rows() == n );
  35. matrix_type LU;
  36. IVector indx;
  37. Vector xv(n);
  38. Vector iv(n);
  39. for(int i=0; i < n;++i)
  40. {
  41. iv[i] = b[i][0];
  42. }
  43. // LU-Zerlegung mit Pivotisierung
  44. LUDecompositionPacked(A,LU,indx,true);
  45. // Lösung M*x1=i1
  46. xv = LUSolve(LU,indx,iv);
  47. x=matrix_type(n,1);
  48. for(int i=0; i < n;++i)
  49. {
  50. x[i][0] = xv[i];
  51. }
  52. }
  53. void getEig(const opt::matrix_type &A, opt::matrix_type &L, opt::matrix_type &V)
  54. {
  55. int n= A.cols();
  56. L = matrix_type(n,1);
  57. V = matrix_type(n,n);
  58. Vector l(n);
  59. Eigenvalue(A,l,V);
  60. for(int i=0; i < n;++i)
  61. {
  62. L[i][0] = l[i];
  63. }
  64. }