Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/deepcpp/algorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ VectorXd projected_gradient_method(
double tolerance)
{
double step_size = 1 / mat.norm();
VectorXd x_old = projection(target);
VectorXd x_old = projection(initial_guess);
for (int i = 0; i < max_iterations; ++i)
{
VectorXd x_new = projection(x_old - step_size * (mat * x_old - target));
Expand Down
13 changes: 12 additions & 1 deletion tests/deepc/test_math.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import unittest
import numpy as np
from deepc.math import left_pseudoinverse, right_pseudoinverse, hankel_matrix
from deepc.math import left_pseudoinverse, right_pseudoinverse, hankel_matrix, projected_gradient_method


class TestLeftPseudoinverse(unittest.TestCase):
Expand Down Expand Up @@ -76,6 +76,17 @@ def test_size_3_and_2_rows(self):
def test_size_3_and_3_rows(self):
np.testing.assert_array_equal(hankel_matrix(3, [(1, 2), (3, 4), (5, 6)]), [[1], [2], [3], [4], [5], [6]])

class TestProjectedGradientMethod(unittest.TestCase):
def test_1x1(self):
mat = np.array([[1, 0], [0, 1]])
x_ini = np.array([1, 1])
target = np.array([0, 0])
def constrain(x):
return x

res = projected_gradient_method(mat, x_ini, target, constrain)

np.testing.assert_array_almost_equal(res, np.array([0, 0]), decimal=5)

if __name__ == "__main__":
unittest.main()
4 changes: 2 additions & 2 deletions tests/deepcpp/test_algorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ TEST(HankelMatrix_2D_data, Size_3_and_3_row)
EXPECT_EQ(res, expected);
}

TEST(Algorithm, ProjectedGradientMethod)
TEST(Algorithm, projected_gradient_method)
{
auto mat = Matrix({1, 0}, {0, 1});
auto initial_guess = Vector(1, 1);
Expand All @@ -180,5 +180,5 @@ TEST(Algorithm, ProjectedGradientMethod)

auto res = projected_gradient_method(mat, initial_guess, target, projection);

EXPECT_EQ(res, Vector(0, 0));
EXPECT_LE(res.norm(), 1e-6);
}