UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
SparseMatrix.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2#pragma once
3
4#include "Chaos/Map.h"
5#include "Chaos/VectorND.h"
6
7namespace Chaos
8{
9template<class T>
11{
12 public:
13 SparseMatrix(const int32 size)
14 : MSize(size) {}
16
17 T operator()(const int32 i, const int32 j)
18 {
19 int32 Key = i * MSize + j;
20 if (!MValues.Contains(Key))
21 {
22 MValues.Add(Key, T());
23 if (!RowToIndicesMap.Contains(i))
24 {
25 RowToIndicesMap.Add(i, {j});
26 }
27 else
28 {
29 RowToIndicesMap[i].Add(j);
30 }
31 }
32 return MValues[Key];
33 }
34 const T operator()(const int32 i, const int32 j) const
35 {
36 int32 Key = i * MSize + j;
37 check(MValues.Contains(Key));
38 return MValues[Key];
39 }
41 {
42 check(vector.Num() == MSize);
43 VectorND<T> result(MSize);
44 for (int32 i = 0; i < MSize; ++i)
45 {
46 result[i] = 0;
47 if (!RowToIndicesMap.Contains(i))
48 continue;
49 for (auto j : RowToIndicesMap[i])
50 {
51 result[i] += (*this)(i, j) * vector[j];
52 }
53 }
54 return result;
55 }
56
57 private:
58 int32 MSize;
59 TMap<int32, TArray<int32>> RowToIndicesMap;
60 TMap<int32, T> MValues;
61};
62}
#define check(expr)
Definition AssertionMacros.h:314
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
Definition SparseMatrix.h:11
VectorND< T > operator*(const VectorND< T > &vector) const
Definition SparseMatrix.h:40
T operator()(const int32 i, const int32 j)
Definition SparseMatrix.h:17
const T operator()(const int32 i, const int32 j) const
Definition SparseMatrix.h:34
SparseMatrix(const int32 size)
Definition SparseMatrix.h:13
~SparseMatrix()
Definition SparseMatrix.h:15
Definition VectorND.h:10
Definition UnrealString.h.inl:34
Definition SkeletalMeshComponent.h:307