philsupertramp/game-math
Loading...
Searching...
No Matches
Differentiation.h
Go to the documentation of this file.
1
36#pragma once
37
38#include "../../Matrix.h"
39#include "../../matrix_utils.h"
40
41#include "../utils.h"
42
54 auto df = zeros(x.rows(), x.columns());
55 auto dy = y.GetSlice(1, y.rows() - 1, 0, y.columns() - 1) - y.GetSlice(0, y.rows() - 2, 0, y.columns() - 1);
56 auto dx = x.GetSlice(1, x.rows() - 1, 0, x.columns() - 1) - x.GetSlice(0, x.rows() - 2, 0, x.columns() - 1);
57 auto res = HadamardDiv(dy, dx);
58 //for(size_t i = 0; i < df.rows() - 1; ++i) { df.SetRow(i, res.GetSlice(i, i, 0, res.columns() - 1)); }
59 df.SetSlice(0, df.rows() - 2, 0, df.columns() - 1, res);
60 return df;
61}
62
74 auto df = zeros(x.rows(), x.columns());
75 auto dy = y.GetSlice(1, y.rows() - 1, 0, y.columns() - 1) - y.GetSlice(0, y.rows() - 2, 0, y.columns() - 1);
76 auto dx = x.GetSlice(1, x.rows() - 1, 0, x.columns() - 1) - x.GetSlice(0, x.rows() - 2, 0, x.columns() - 1);
77 auto res = HadamardDiv(dy, dx);
78 df.SetSlice(1, df.rows() - 1, 0, df.columns() - 1, res);
79 return df;
80}
81
93 auto df = zeros(x.rows(), x.columns());
94 auto dy = y.GetSlice(2, y.rows() - 1, 0, y.columns() - 1) - y.GetSlice(0, y.rows() - 3, 0, y.columns() - 1);
95 auto dx = x.GetSlice(2, x.rows() - 1, 0, x.columns() - 1) - x.GetSlice(0, x.rows() - 3, 0, x.columns() - 1);
96 auto res = HadamardDiv(dy, dx);
97 df.SetSlice(1, df.rows() - 2, 0, df.columns() - 1, res);
98 return df;
99}
100
111 auto df = zeros(x.rows(), x.columns());
112 auto dy = (3 * y.GetSlice(2, y.rows() - 1, 0, y.columns() - 1))
113 - (4 * y.GetSlice(1, y.rows() - 2, 0, y.columns() - 1)) + y.GetSlice(0, y.rows() - 3, 0, y.columns() - 1);
114 auto dx = x.GetSlice(2, x.rows() - 1, 0, x.columns() - 1) - x.GetSlice(0, x.rows() - 3, 0, x.columns() - 1);
115 auto res = HadamardDiv(dy, dx);
116 df.SetSlice(2, df.rows() - 1, 0, df.columns() - 1, res);
117 return df;
118}
119
130 auto df = zeros(x.rows(), x.columns());
131 auto dy = (-1 * y.GetSlice(4, y.rows() - 1)) + (8 * y.GetSlice(3, y.rows() - 2)) - (8 * y.GetSlice(1, y.rows() - 4))
132 + y.GetSlice(0, y.rows() - 5);
133 auto dx = 3.0 * (x.GetSlice(4, x.rows() - 1) - x.GetSlice(0, x.rows() - 5));
134 auto res = HadamardDiv(dy, dx);
135 df.SetSlice(2, df.rows() - 3, 0, df.columns() - 1, res);
136 return df;
137}
138
Matrix< double > forwardDiff(const Matrix< double > &x, const Matrix< double > &y)
Definition: Differentiation.h:53
Matrix< double > backwardDiff(const Matrix< double > &x, const Matrix< double > &y)
Definition: Differentiation.h:73
Matrix< double > centralDiff4(const Matrix< double > &x, const Matrix< double > &y)
Definition: Differentiation.h:129
Matrix< double > centralDiff(const Matrix< double > &x, const Matrix< double > &y)
Definition: Differentiation.h:92
Matrix< double > backwardDiff2(const Matrix< double > &x, const Matrix< double > &y)
Definition: Differentiation.h:110
Definition: Matrix.h:42
size_t rows() const
Definition: Matrix.h:193
size_t columns() const
Definition: Matrix.h:198
Matrix GetSlice(size_t rowStart) const
Definition: Matrix.h:609
Matrix< T > HadamardDiv(const Matrix< T > &lhs, const Matrix< T > &rhs)
Definition: matrix_utils.h:36
Matrix< double > zeros(size_t rows, size_t columns, size_t elements=1)