<commit_before># Test osqp python module
import osqp
# import osqppurepy as osqp
import numpy as np
from scipy import sparse

# Unit Test
import unittest
import numpy.testing as nptest
import shutil as sh


class codegen_matrices_tests(unittest.TestCase):

    def setUp(self):
        # Simple QP problem
        self.P = sparse.csc_matrix([[11, 0], [0, 0.1]])
        self.P_new = sparse.eye(2).tocsc()
        self.q = np.array([3, 4])
        self.A = sparse.csc_matrix([[-1, 0], [0, -1], [-1, -3],
                                    [2, 5], [3, 4]])
        self.A_new = sparse.csc_matrix([[-1, 0], [0, -1], [-2, -2],
                                        [2, 5], [3, 4]])
        self.u = np.array([0, 0, -15, 100, 80])
        self.l = -np.inf * np.ones(len(self.u))
        self.n = self.P.shape[0]
        self.m = self.A.shape[0]
        self.opts = {'verbose': False,
                     'eps_abs': 1e-08,
                     'eps_rel': 1e-08,
                     'alpha': 1.6,
                     'max_iter': 3000,
                     'warm_start': True}
        self.model = osqp.OSQP()
        self.model.setup(P=self.P, q=self.q, A=self.A, l=self.l, u=self.u,
                         **self.opts)

    def test_solve(self):
        # Generate the code
        self.model.codegen('code2', python_ext_name='mat_emosqp',
                           force_rewrite=True, parameters='matrices')

        # sh.rmtree('code2')
        import mat_emosqp

        # Solve problem
        x, y, _, _, _ = mat_emosqp.solve()

        # Assert close
        nptest.assert_array_almost_equal(x, np.array([0., 5.]), decimal=5)
        nptest.assert_array_almost_equal(
            y, np.array([1.5, 0., 1.5, 0., 0.]), decimal=5)

    def test_update_P(self):
        # Generate the code (TODO: REMOVE)
        self.model.codegen('code2', python_ext_name='mat_emosqp',
                           force_rewrite=True, parameters='matrices')

        import mat_emosqp

        # Update matrix P
        Pnew_triu = sparse.triu(self.P_new).tocsc()
        Px = Pnew_triu.data
        Px_idx = np.arange(Pnew_triu.nnz)
        mat_emosqp.update_P(Px, Px_idx, len(Px))

        # Solve problem
        x, y, _, _, _ = mat_emosqp.solve()

        # Assert close
        nptest.assert_array_almost_equal(x, np.array([0., 5.]), decimal=5)
        nptest.assert_array_almost_equal(
            y, np.array([0., 0., 3., 0., 0.]), decimal=5)

        # Update matrix P to the original value
        P_triu = sparse.triu(self.P).tocsc()
        Px = P_triu.data
        Px_idx = np.arange(P_triu.nnz)
        mat_emosqp.update_P(Px, Px_idx, len(Px))

    def test_update_P_allind(self):
        import mat_emosqp

        # Update matrix P
        Pnew_triu = sparse.triu(self.P_new).tocsc()
        Px = Pnew_triu.data<commit_msg>Fixed codegen matrices tests<commit_after># Test osqp python module
import osqp
# import osqppurepy as osqp
import numpy as np
from scipy import sparse

# Unit Test
import unittest
import numpy.testing as nptest
import shutil as sh


class codegen_matrices_tests(unittest.TestCase):

    def setUp(self):
        # Simple QP problem
        self.P = sparse.csc_matrix([[11, 0], [0, 0.1]])
        self.P_new = sparse.eye(2).tocsc()
        self.q = np.array([3, 4])
        self.A = sparse.csc_matrix([[-1, 0], [0, -1], [-1, -3],
                                    [2, 5], [3, 4]])
        self.A_new = sparse.csc_matrix([[-1, 0], [0, -1], [-2, -2],
                                        [2, 5], [3, 4]])
        self.u = np.array([0, 0, -15, 100, 80])
        self.l = -np.inf * np.ones(len(self.u))
        self.n = self.P.shape[0]
        self.m = self.A.shape[0]
        self.opts = {'verbose': False,
                     'eps_abs': 1e-08,
                     'eps_rel': 1e-08,
                     'alpha': 1.6,
                     'max_iter': 3000,
                     'warm_start': True}
        self.model = osqp.OSQP()
        self.model.setup(P=self.P, q=self.q, A=self.A, l=self.l, u=self.u,
                         **self.opts)

    def test_solve(self):
        # Generate the code
        self.model.codegen('code2', python_ext_name='mat_emosqp',
                           force_rewrite=True, parameters='matrices')

        sh.rmtree('code2')
        import mat_emosqp

        # Solve problem
        x, y, _, _, _ = mat_emosqp.solve()

        # Assert close
        nptest.assert_array_almost_equal(x, np.array([0., 5.]), decimal=5)
        nptest.assert_array_almost_equal(
            y, np.array([1.5, 0., 1.5, 0., 0.]), decimal=5)

    def test_update_P(self):
        import mat_emosqp

        # Update matrix P
        Pnew_triu = sparse.triu(self.P_new).tocsc()
        Px = Pnew_triu.data
        Px_idx = np.arange(Pnew_triu.nnz)
        mat_emosqp.update_P(Px, Px_idx, len(Px))

        # Solve problem
        x, y, _, _, _ = mat_emosqp.solve()

        # Assert close
        nptest.assert_array_almost_equal(x, np.array([0., 5.]), decimal=5)
        nptest.assert_array_almost_equal(
            y, np.array([0., 0., 3., 0., 0.]), decimal=5)

        # Update matrix P to the original value
        P_triu = sparse.triu(self.P).tocsc()
        Px = P_triu.data
        Px_idx = np.arange(P_triu.nnz)
        mat_emosqp.update_P(Px, Px_idx, len(Px))

    def test_update_P_allind(self):
        import mat_emosqp

        # Update matrix P
        Pnew_triu = sparse.triu(self.P_new).tocsc()
        Px = Pnew_triu.data
{'fraction_non_alphanumeric': 0.14020054694621695, 'fraction_numerical': 0.02716499544211486, 'mean_word_length': 1.7861858811579483, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1678234', 'n_tokens_mistral': 2085, 'n_tokens_neox': 1938, 'n_words': 405}