<commit_before>/*
   Copyright 2020 <NAME>

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

package org.gavrog.jane.compounds;

import java.lang.ArithmeticException;
import java.util.Arrays;

import org.gavrog.box.simple.TaskController;
import org.gavrog.jane.numbers.Rational;
import org.gavrog.jane.numbers.Whole;


public class ModularSolver {

    public static int modularInverse(final int a, final int m) {
        int t = 0;
        int t1 = 1;
        int r = m;
        int r1 = a;
        int tmp;

        while (r1 != 0) {
            final int q = r / r1;

            tmp = t1;
            t1 = t - q * t1;
            t = tmp;

            tmp = r1;
            r1 = r - q * r1;
            r = tmp;
        }

        if (r != 1)
            throw new ArithmeticException(a + " has no inverse modulo " + m);

        if (t < 0)
            return t + m;
        else
            return t;
    }


    public static int[][] modularRowEchelonForm(final int[][] M, final int m) {
        // --- used for answering external cancel request
        final TaskController controller = TaskController.getInstance();

        final int nrows = M.length;
        final int ncols = M[0].length;

        final int A[][] = new int[nrows][ncols];
        for (int i = 0; i < nrows; ++i) {
            for (int j = 0; j < ncols; ++j) {
                final int a = M[i][j] % m;
                if (a < 0)
                    A[i][j] = a + m;
                else
                    A[i][j] = a;
            }
        }

        int row = 0;

        for (int col = 0; col < ncols; ++col) {
            // --- throw if the task controller received a cancel request
            controller.bailOutIfCancelled();

            int r = row;
            while (r < nrows && A[r][col] == 0)
                ++r;

            if (r >= nrows)
                continue;

            if (r != row) {
                final int[] tmp = A[r];
                A[r] = A[row];
                A[row] = tmp;
            }

            final long f = modularInverse(A[row][col], m);
            for (int j = col; j < ncols; ++j)
                A[row][j] = (int) ((A[row][j] * f) % m);

            for (int i = 0; i < nrows; ++i) {
                if (i == row || A[i][col] == 0)
                    continue;

                final long g = A[i][col];
                for (int j = col; j < ncols; ++j) {
                    if (A[row][j] != 0) {
                        A[i][j] = (int)
                            ((m - (A[row][j] * g) % m + A[i][j]) % m);
                    }
                }
            }

            ++row;
        }

        return A;
    }


    public static int[][] modularMatrixInverse(final int[][] M, final int m) {
        final int n = M.length;

        if (M[0].length != n)
            throw new ArithmeticException("matrix must be quadratic");

        final int A[][] = new int[n][n+n];
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                A[i][j] = M[i][j];
            }
            A[i][n + i] = 1;
        }

        final int E[][] = modularRowEchelonForm(A, m);

        for (int i = 0; i < n; ++i)
            if (E[i][i] != 1)
                throw new ArithmeticException("matrix is not invertible");

        final int R[][] = new int[n][n];
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                R[i][j] = E[i][j + n];
            }
        }

        return R;<commit_msg>ModularSolver: reduce temporary matrix allocation<commit_after>/*
   Copyright 2020 <NAME>

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

package org.gavrog.jane.compounds;

import java.lang.ArithmeticException;
import java.util.Arrays;

import org.gavrog.box.simple.TaskController;
import org.gavrog.jane.numbers.Rational;
import org.gavrog.jane.numbers.Whole;


public class ModularSolver {

    public static int modularInverse(final int a, final int m) {
        int t = 0;
        int t1 = 1;
        int r = m;
        int r1 = a;
        int tmp;

        while (r1 != 0) {
            final int q = r / r1;

            tmp = t1;
            t1 = t - q * t1;
            t = tmp;

            tmp = r1;
            r1 = r - q * r1;
            r = tmp;
        }

        if (r != 1)
            throw new ArithmeticException(a + " has no inverse modulo " + m);

        if (t < 0)
            return t + m;
        else
            return t;
    }


    public static void modularRowEchelonFormInPlace
        (final int[][] A, final int m)
    {
        // --- used for answering external cancel request
        final TaskController controller = TaskController.getInstance();

        final int nrows = A.length;
        final int ncols = A[0].length;

        int row = 0;

        for (int col = 0; col < ncols; ++col) {
            // --- throw if the task controller received a cancel request
            controller.bailOutIfCancelled();

            int r = row;
            while (r < nrows && A[r][col] == 0)
                ++r;

            if (r >= nrows)
                continue;

            if (r != row) {
                final int[] tmp = A[r];
                A[r] = A[row];
                A[row] = tmp;
            }

            final long f = modularInverse(A[row][col], m);
            for (int j = col; j < ncols; ++j)
                A[row][j] = (int) ((A[row][j] * f) % m);

            for (int i = 0; i < nrows; ++i) {
                if (i == row || A[i][col] == 0)
                    continue;

                final long g = A[i][col];
                for (int j = col; j < ncols; ++j) {
                    if (A[row][j] != 0) {
                        A[i][j] = (int)
                            ((m - (A[row][j] * g) % m + A[i][j]) % m);
                    }
                }
            }

            ++row;
        }
    }


    public static int[][] modularRowEchelonForm(final int[][] M, final int m) {
        final int nrows = M.length;
        final int ncols = M[0].length;

        final int A[][] = new int[nrows][ncols];
        for (int i = 0; i < nrows; ++i)
            for (int j = 0; j < ncols; ++j)
                A[i][j] = M[i][j];

        modularRowEchelonFormInPlace(A, m);
        return A;
    }


    public static int[][] modularMatrixInverse(final int[][] M, final int m) {
        final int n = M.length;

        if (M[0].length != n)
            throw new ArithmeticException("matrix must be quadratic");

        final int A[][] = new int[n][n+n];
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                A[i][j] = M[i][j];
            }
            A[i][n + i] = 1;
        }

        modularRowEchelonFormInPlace(A, m);

        for (int i = 0; i < n; ++i)
            if (A[i][i] != 1)
                throw new ArithmeticException("matrix is not invertible");

        final int R[][] = new int[n][n];
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                R[i][j] = A[i][j + n];
            }
        }

        return R;
{'fraction_non_alphanumeric': 0.12977382276603633, 'fraction_numerical': 0.010134717587442837, 'mean_word_length': 1.381400824014126, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7480280', 'n_tokens_mistral': 2658, 'n_tokens_neox': 2475, 'n_words': 836}