File: right_kernel.c

package info (click to toggle)
calcium 0.4.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 4,756 kB
  • sloc: ansic: 62,836; python: 2,827; sh: 518; makefile: 163
file content (100 lines) | stat: -rw-r--r-- 2,415 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
    Copyright (C) 2020 Fredrik Johansson

    This file is part of Calcium.

    Calcium is free software: you can redistribute it and/or modify it under
    the terms of the GNU Lesser General Public License (LGPL) as published
    by the Free Software Foundation; either version 2.1 of the License, or
    (at your option) any later version.  See <http://www.gnu.org/licenses/>.
*/

#include "ca_mat.h"

int
ca_mat_right_kernel(ca_mat_t X, const ca_mat_t A, ca_ctx_t ctx)
{
    slong i, j, k, m, n, rank, nullity;
    slong *p;
    slong *pivots;
    slong *nonpivots;
    ca_mat_t tmp;
    int success;

    m = ca_mat_nrows(A);
    n = ca_mat_ncols(A);

    p = flint_malloc(sizeof(slong) * FLINT_MAX(m, n));

    ca_mat_init(tmp, m, n, ctx);
    success = ca_mat_rref(&rank, tmp, A, ctx);
    nullity = n - rank;

    if (!success)
        goto cleanup;

    ca_mat_clear(X, ctx);
    ca_mat_init(X, n, nullity, ctx);

    success = 1;

    if (rank == 0)
    {
        for (i = 0; i < nullity; i++)
            ca_one(ca_mat_entry(X, i, i), ctx);
    }
    else if (nullity)
    {
        pivots = p;             /* length = rank */
        nonpivots = p + rank;   /* length = nullity */

        for (i = j = k = 0; i < rank; i++)
        {
            while (1)
            {
                /* Todo: this should not be T_UNKNOWN. Should we save
                   the pivot data in the lu algorithm? */
                truth_t is_zero = ca_check_is_zero(ca_mat_entry(tmp, i, j), ctx);

                if (is_zero == T_FALSE)
                {
                    break;
                }
                else if (is_zero == T_TRUE)
                {
                    nonpivots[k] = j;
                    k++;
                    j++;
                }
                else
                {
                    success = 0;
                    goto cleanup;
                }
            }

            pivots[i] = j;
            j++;
        }
        while (k < n - rank)
        {
            nonpivots[k] = j;
            k++;
            j++;
        }

        for (i = 0; i < nullity; i++)
        {
            for (j = 0; j < rank; j++)
                ca_neg(ca_mat_entry(X, pivots[j], i), ca_mat_entry(tmp, j, nonpivots[i]), ctx);

            ca_one(ca_mat_entry(X, nonpivots[i], i), ctx);
        }
    }

cleanup:
    flint_free(p);
    ca_mat_clear(tmp, ctx);

    return success;
}