LinearAlgebra.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #include <AzCore/base.h>
  10. #include <AzCore/std/containers/vector.h>
  11. // This provides just the functionality for arbitrary dimension matrices and vectors that is required by this gem.
  12. // It is not intended to be a complete or optimized implementation.
  13. // If we add support for arbitrary dimension matrices or use a third party library, that should replace what is here.
  14. namespace NumericalMethods
  15. {
  16. using ScalarVariable = double;
  17. //! Class for arbitrary sized vectors, providing only the functionality required by the numerical methods supported
  18. //! in this gem.
  19. class VectorVariable
  20. {
  21. public:
  22. VectorVariable() = default;
  23. explicit VectorVariable(AZ::u32 dimension);
  24. static VectorVariable CreateFromVector(const AZStd::vector<double>& values);
  25. AZ::u32 GetDimension() const;
  26. double& operator[](AZ::u32 index);
  27. double operator[](AZ::u32 index) const;
  28. VectorVariable operator+(const VectorVariable& rhs) const;
  29. VectorVariable operator+=(const VectorVariable& rhs);
  30. VectorVariable operator-() const;
  31. VectorVariable operator-(const VectorVariable& rhs) const;
  32. VectorVariable operator-=(const VectorVariable& rhs);
  33. VectorVariable operator*(const double rhs) const;
  34. double Norm() const;
  35. double Dot(const VectorVariable& rhs) const;
  36. const AZStd::vector<double>& GetValues() const;
  37. private:
  38. AZStd::vector<double> m_values;
  39. };
  40. VectorVariable operator*(const double lhs, const VectorVariable& rhs);
  41. //! Class for arbitrary sized matrices, providing only the functionality required by the numerical methods supported
  42. //! in this gem.
  43. class MatrixVariable
  44. {
  45. public:
  46. MatrixVariable() = default;
  47. MatrixVariable(AZ::u32 numRows, AZ::u32 numColumns);
  48. double& Element(AZ::u32 row, AZ::u32 column);
  49. double Element(AZ::u32 row, AZ::u32 column) const;
  50. AZ::u32 GetNumRows() const;
  51. AZ::u32 GetNumColumns() const;
  52. MatrixVariable operator+(const MatrixVariable& rhs) const;
  53. MatrixVariable operator+=(const MatrixVariable& rhs);
  54. MatrixVariable operator-(const MatrixVariable& rhs) const;
  55. MatrixVariable operator/(const double divisor) const;
  56. private:
  57. AZStd::vector<double> m_values;
  58. AZ::u32 m_numRows = 0;
  59. AZ::u32 m_numColumns = 0;
  60. };
  61. VectorVariable operator*(const MatrixVariable& lhs, const VectorVariable& rhs);
  62. MatrixVariable operator*(const MatrixVariable& lhs, const MatrixVariable& rhs);
  63. MatrixVariable operator*(double lhs, const MatrixVariable& rhs);
  64. MatrixVariable OuterProduct(const VectorVariable& x, const VectorVariable& y);
  65. } // namespace NumericalMethods