123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296 |
- /*
- * Copyright (c) Contributors to the Open 3D Engine Project.
- * For complete copyright and license terms please see the LICENSE at the root of this distribution.
- *
- * SPDX-License-Identifier: Apache-2.0 OR MIT
- *
- */
- #include <LinearAlgebra.h>
- #include <AzCore/Memory/SystemAllocator.h>
- #include <AzCore/Math/MathUtils.h>
- namespace NumericalMethods
- {
- VectorVariable::VectorVariable(AZ::u32 dimension)
- {
- m_values.resize(dimension, 0.0);
- }
- VectorVariable VectorVariable::CreateFromVector(const AZStd::vector<double>& values)
- {
- VectorVariable result;
- result.m_values = values;
- return result;
- }
- AZ::u32 VectorVariable::GetDimension() const
- {
- return static_cast<AZ::u32>(m_values.size());
- }
- double& VectorVariable::operator[](AZ::u32 index)
- {
- AZ_Assert(index < m_values.size(), "Invalid VectorVariable index.");
- return m_values[index];
- }
- double VectorVariable::operator[](AZ::u32 index) const
- {
- AZ_Assert(index < m_values.size(), "Invalid VectorVariable index.");
- return m_values[index];
- }
- VectorVariable VectorVariable::operator+(const VectorVariable& rhs) const
- {
- const AZ::u32 dimension = GetDimension();
- AZ_Assert(dimension == rhs.GetDimension(), "VectorVariable dimensions do not match.");
- VectorVariable result(dimension);
- for (AZ::u32 i = 0; i < dimension; i++)
- {
- result.m_values[i] = m_values[i] + rhs[i];
- }
- return result;
- }
- VectorVariable VectorVariable::operator+=(const VectorVariable& rhs)
- {
- const AZ::u32 dimension = GetDimension();
- AZ_Assert(dimension == rhs.GetDimension(), "VectorVariable dimensions do not match.");
- for (AZ::u32 i = 0; i < dimension; i++)
- {
- m_values[i] += rhs[i];
- }
- return *this;
- }
- VectorVariable VectorVariable::operator-() const
- {
- const AZ::u32 dimension = GetDimension();
- VectorVariable result(dimension);
- for (AZ::u32 i = 0; i < dimension; i++)
- {
- result[i] = -m_values[i];
- }
- return result;
- }
- VectorVariable VectorVariable::operator-(const VectorVariable& rhs) const
- {
- const AZ::u32 dimension = GetDimension();
- AZ_Assert(dimension == rhs.GetDimension(), "VectorVariable dimensions do not match.");
- VectorVariable result(dimension);
- for (AZ::u32 i = 0; i < dimension; i++)
- {
- result[i] = m_values[i] - rhs[i];
- }
- return result;
- }
- VectorVariable VectorVariable::operator-=(const VectorVariable& rhs)
- {
- const AZ::u32 dimension = GetDimension();
- AZ_Assert(dimension == rhs.GetDimension(), "VectorVariable dimensions do not match.");
- for (AZ::u32 i = 0; i < dimension; i++)
- {
- m_values[i] -= rhs[i];
- }
- return *this;
- }
- VectorVariable VectorVariable::operator*(const double rhs) const
- {
- const AZ::u32 dimension = GetDimension();
- VectorVariable result(dimension);
- for (AZ::u32 i = 0; i < dimension; i++)
- {
- result[i] = m_values[i] * rhs;
- }
- return result;
- }
- double VectorVariable::Norm() const
- {
- const AZ::u32 dimension = GetDimension();
- double sumSquares = 0.0;
- for (AZ::u32 i = 0; i < dimension; i++)
- {
- sumSquares += m_values[i] * m_values[i];
- }
- return sqrt(sumSquares);
- }
- double VectorVariable::Dot(const VectorVariable& rhs) const
- {
- const AZ::u32 dimension = GetDimension();
- AZ_Assert(dimension == rhs.GetDimension(), "VectorVariable dimensions do not match.");
- double result = 0.0;
- for (AZ::u32 i = 0; i < dimension; i++)
- {
- result += m_values[i] * rhs[i];
- }
- return result;
- }
- const AZStd::vector<double>& VectorVariable::GetValues() const
- {
- return m_values;
- }
- VectorVariable operator*(const double lhs, const VectorVariable& rhs)
- {
- const AZ::u32 dimension = rhs.GetDimension();
- VectorVariable result(dimension);
- for (AZ::u32 i = 0; i < dimension; i++)
- {
- result[i] = lhs * rhs[i];
- }
- return result;
- }
- MatrixVariable::MatrixVariable(AZ::u32 numRows, AZ::u32 numColumns)
- {
- m_numRows = numRows;
- m_numColumns = numColumns;
- m_values.clear();
- m_values.resize(m_numRows * m_numColumns, 0.0);
- }
- double& MatrixVariable::Element(AZ::u32 row, AZ::u32 column)
- {
- AZ_Assert(row < m_numRows && column < m_numColumns, "Invalid matrix index.");
- return m_values[row * m_numColumns + column];
- }
- double MatrixVariable::Element(AZ::u32 row, AZ::u32 column) const
- {
- AZ_Assert(row < m_numRows && column < m_numColumns, "Invalid matrix index.");
- return m_values[row * m_numColumns + column];
- }
- AZ::u32 MatrixVariable::GetNumRows() const
- {
- return m_numRows;
- }
- AZ::u32 MatrixVariable::GetNumColumns() const
- {
- return m_numColumns;
- }
- MatrixVariable MatrixVariable::operator+(const MatrixVariable& rhs) const
- {
- AZ_Assert(m_numRows == rhs.m_numRows && m_numColumns == rhs.m_numColumns, "Matrix dimensions do not match.");
- MatrixVariable result(m_numRows, m_numColumns);
- for (AZ::u32 row = 0; row < m_numRows; row++)
- {
- for (AZ::u32 column = 0; column < m_numColumns; column++)
- {
- result.Element(row, column) = Element(row, column) + rhs.Element(row, column);
- }
- }
- return result;
- }
- MatrixVariable MatrixVariable::operator+=(const MatrixVariable& rhs)
- {
- AZ_Assert(m_numRows == rhs.m_numRows && m_numColumns == rhs.m_numColumns, "Matrix dimensions do not match.");
- for (AZ::u32 row = 0; row < m_numRows; row++)
- {
- for (AZ::u32 column = 0; column < m_numColumns; column++)
- {
- Element(row, column) += rhs.Element(row, column);
- }
- }
- return *this;
- }
- MatrixVariable MatrixVariable::operator-(const MatrixVariable& rhs) const
- {
- MatrixVariable result(m_numRows, m_numColumns);
- for (AZ::u32 row = 0; row < m_numRows; row++)
- {
- for (AZ::u32 column = 0; column < m_numColumns; column++)
- {
- result.Element(row, column) = Element(row, column) - rhs.Element(row, column);
- }
- }
- return result;
- }
- MatrixVariable MatrixVariable::operator/(const double divisor) const
- {
- MatrixVariable result(m_numRows, m_numColumns);
- for (AZ::u32 row = 0; row < m_numRows; row++)
- {
- for (AZ::u32 column = 0; column < m_numColumns; column++)
- {
- result.Element(row, column) = Element(row, column) / divisor;
- }
- }
- return result;
- }
- VectorVariable operator*(const MatrixVariable& lhs, const VectorVariable& rhs)
- {
- AZ_Assert(lhs.GetNumColumns() == rhs.GetDimension(), "Matrix and vector dimensions do not match.");
- VectorVariable result(lhs.GetNumRows());
- for (AZ::u32 row = 0; row < lhs.GetNumRows(); row++)
- {
- result[row] = 0.0;
- for (AZ::u32 column = 0; column < lhs.GetNumColumns(); column++)
- {
- result[row] += lhs.Element(row, column) * rhs[column];
- }
- }
- return result;
- }
- MatrixVariable operator*(const MatrixVariable& lhs, const MatrixVariable& rhs)
- {
- AZ_Assert(lhs.GetNumColumns() == rhs.GetNumRows(), "Invalid matrix dimensions for multiplication.");
- MatrixVariable result(lhs.GetNumRows(), rhs.GetNumColumns());
- for (AZ::u32 row = 0; row < lhs.GetNumRows(); row++)
- {
- for (AZ::u32 column = 0; column < rhs.GetNumColumns(); column++)
- {
- result.Element(row, column) = 0.0;
- for (AZ::u32 i = 0; i < lhs.GetNumColumns(); i++)
- {
- result.Element(row, column) += lhs.Element(row, i) * rhs.Element(i, column);
- }
- }
- }
- return result;
- }
- MatrixVariable operator*(double lhs, const MatrixVariable& rhs)
- {
- MatrixVariable result(rhs.GetNumRows(), rhs.GetNumColumns());
- const AZ::u32 numRows = rhs.GetNumRows();
- const AZ::u32 numColumns = rhs.GetNumColumns();
- for (AZ::u32 row = 0; row < numRows; row++)
- {
- for (AZ::u32 column = 0; column < numColumns; column++)
- {
- result.Element(row, column) = lhs * rhs.Element(row, column);
- }
- }
- return result;
- }
- MatrixVariable OuterProduct(const VectorVariable& x, const VectorVariable& y)
- {
- MatrixVariable result(x.GetDimension(), y.GetDimension());
- for (AZ::u32 r = 0; r < x.GetDimension(); r++)
- {
- for (AZ::u32 c = 0; c < y.GetDimension(); c++)
- {
- result.Element(r, c) = x[r] * y[c];
- }
- }
- return result;
- }
- } // namespace NumericalMethods
|