isl_mat_lin_to_aff: fix error handling
[platform/upstream/isl.git] / isl_mat.c
index 5cc2526..244eb0c 100644 (file)
--- a/isl_mat.c
+++ b/isl_mat.c
@@ -1,3 +1,12 @@
+/*
+ * Copyright 2008-2009 Katholieke Universiteit Leuven
+ *
+ * Use of this software is governed by the GNU LGPLv2.1 license
+ *
+ * Written by Sven Verdoolaege, K.U.Leuven, Departement
+ * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
+ */
+
 #include "isl_dim.h"
 #include "isl_seq.h"
 #include "isl_mat.h"
@@ -218,7 +227,7 @@ struct isl_vec *isl_mat_vec_product(struct isl_mat *mat, struct isl_vec *vec)
        if (!mat || !vec)
                goto error;
 
-       isl_assert(ctx, mat->n_col == vec->size, goto error);
+       isl_assert(mat->ctx, mat->n_col == vec->size, goto error);
 
        prod = isl_vec_alloc(mat->ctx, mat->n_row);
        if (!prod)
@@ -236,6 +245,35 @@ error:
        return NULL;
 }
 
+__isl_give isl_vec *isl_mat_vec_inverse_product(__isl_take isl_mat *mat,
+       __isl_take isl_vec *vec)
+{
+       struct isl_mat *vec_mat;
+       int i;
+
+       if (!mat || !vec)
+               goto error;
+       vec_mat = isl_mat_alloc(vec->ctx, vec->size, 1);
+       if (!vec_mat)
+               goto error;
+       for (i = 0; i < vec->size; ++i)
+               isl_int_set(vec_mat->row[i][0], vec->el[i]);
+       vec_mat = isl_mat_inverse_product(mat, vec_mat);
+       isl_vec_free(vec);
+       if (!vec_mat)
+               return NULL;
+       vec = isl_vec_alloc(vec_mat->ctx, vec_mat->n_row);
+       if (vec)
+               for (i = 0; i < vec->size; ++i)
+                       isl_int_set(vec->el[i], vec_mat->row[i][0]);
+       isl_mat_free(vec_mat);
+       return vec;
+error:
+       isl_mat_free(mat);
+       isl_vec_free(vec);
+       return NULL;
+}
+
 struct isl_vec *isl_vec_mat_product(struct isl_vec *vec, struct isl_mat *mat)
 {
        int i, j;
@@ -244,7 +282,7 @@ struct isl_vec *isl_vec_mat_product(struct isl_vec *vec, struct isl_mat *mat)
        if (!mat || !vec)
                goto error;
 
-       isl_assert(ctx, mat->n_row == vec->size, goto error);
+       isl_assert(mat->ctx, mat->n_row == vec->size, goto error);
 
        prod = isl_vec_alloc(mat->ctx, mat->n_col);
        if (!prod)
@@ -273,14 +311,14 @@ struct isl_mat *isl_mat_aff_direct_sum(struct isl_mat *left,
        if (!left || !right)
                goto error;
 
-       isl_assert(ctx, left->n_row == right->n_row, goto error);
-       isl_assert(ctx, left->n_row >= 1, goto error);
-       isl_assert(ctx, left->n_col >= 1, goto error);
-       isl_assert(ctx, right->n_col >= 1, goto error);
-       isl_assert(ctx,
+       isl_assert(left->ctx, left->n_row == right->n_row, goto error);
+       isl_assert(left->ctx, left->n_row >= 1, goto error);
+       isl_assert(left->ctx, left->n_col >= 1, goto error);
+       isl_assert(left->ctx, right->n_col >= 1, goto error);
+       isl_assert(left->ctx,
            isl_seq_first_non_zero(left->row[0]+1, left->n_col-1) == -1,
            goto error);
-       isl_assert(ctx,
+       isl_assert(left->ctx,
            isl_seq_first_non_zero(right->row[0]+1, right->n_col-1) == -1,
            goto error);
 
@@ -485,7 +523,7 @@ struct isl_mat *isl_mat_lin_to_aff(struct isl_mat *mat)
                return NULL;
        mat2 = isl_mat_alloc(mat->ctx, 1+mat->n_row, 1+mat->n_col);
        if (!mat2)
-               return NULL;
+               goto error;
        isl_int_set_si(mat2->row[0][0], 1);
        isl_seq_clr(mat2->row[0]+1, mat->n_col);
        for (i = 0; i < mat->n_row; ++i) {
@@ -494,6 +532,9 @@ struct isl_mat *isl_mat_lin_to_aff(struct isl_mat *mat)
        }
        isl_mat_free(mat);
        return mat2;
+error:
+       isl_mat_free(mat);
+       return NULL;
 }
 
 static int row_first_non_zero(isl_int **row, unsigned n_row, unsigned col)
@@ -578,7 +619,7 @@ struct isl_mat *isl_mat_inverse_product(struct isl_mat *left,
                if (pivot < 0) {
                        isl_int_clear(a);
                        isl_int_clear(b);
-                       isl_assert(ctx, pivot >= 0, goto error);
+                       isl_assert(left->ctx, pivot >= 0, goto error);
                }
                pivot += row;
                if (pivot != row)
@@ -603,11 +644,11 @@ struct isl_mat *isl_mat_inverse_product(struct isl_mat *left,
                        isl_int_gcd(a, left->row[row][row], left->row[i][row]);
                        isl_int_divexact(b, left->row[i][row], a);
                        isl_int_divexact(a, left->row[row][row], a);
-                       isl_int_neg(a, a);
-                       isl_seq_combine(left->row[i]+row,
-                                       a, left->row[i]+row,
-                                       b, left->row[row]+row,
-                                       left->n_col-row);
+                       isl_int_neg(b, b);
+                       isl_seq_combine(left->row[i] + i,
+                                       a, left->row[i] + i,
+                                       b, left->row[row] + i,
+                                       left->n_col - i);
                        isl_seq_combine(right->row[i], a, right->row[i],
                                        b, right->row[row], right->n_col);
                }
@@ -619,7 +660,7 @@ struct isl_mat *isl_mat_inverse_product(struct isl_mat *left,
                isl_int_lcm(a, a, left->row[row][row]);
        if (isl_int_is_zero(a)){
                isl_int_clear(a);
-               isl_assert(ctx, 0, goto error);
+               isl_assert(left->ctx, 0, goto error);
        }
        for (row = 0; row < left->n_row; ++row) {
                isl_int_divexact(left->row[row][row], a, left->row[row][row]);
@@ -684,7 +725,7 @@ struct isl_mat *isl_mat_right_inverse(struct isl_mat *mat)
                if (pivot < 0) {
                        isl_int_clear(a);
                        isl_int_clear(b);
-                       goto error;
+                       isl_assert(mat->ctx, pivot >= 0, goto error);
                }
                pivot += row;
                if (pivot != row)
@@ -773,8 +814,8 @@ struct isl_mat *isl_mat_swap_cols(struct isl_mat *mat, unsigned i, unsigned j)
        mat = isl_mat_cow(mat);
        if (!mat)
                return NULL;
-       isl_assert(ctx, i < mat->n_col, goto error);
-       isl_assert(ctx, j < mat->n_col, goto error);
+       isl_assert(mat->ctx, i < mat->n_col, goto error);
+       isl_assert(mat->ctx, j < mat->n_col, goto error);
 
        for (r = 0; r < mat->n_row; ++r)
                isl_int_swap(mat->row[r][i], mat->row[r][j]);
@@ -806,7 +847,7 @@ struct isl_mat *isl_mat_product(struct isl_mat *left, struct isl_mat *right)
 
        if (!left || !right)
                goto error;
-       isl_assert(ctx, left->n_col == right->n_row, goto error);
+       isl_assert(left->ctx, left->n_col == right->n_row, goto error);
        prod = isl_mat_alloc(left->ctx, left->n_row, right->n_col);
        if (!prod)
                goto error;
@@ -897,6 +938,7 @@ struct isl_basic_set *isl_basic_set_preimage(struct isl_basic_set *bset,
 
        isl_assert(ctx, bset->dim->nparam == 0, goto error);
        isl_assert(ctx, 1+bset->dim->n_out == mat->n_row, goto error);
+       isl_assert(ctx, mat->n_col > 0, goto error);
 
        if (mat->n_col > mat->n_row)
                bset = isl_basic_set_extend(bset, 0, mat->n_col-1, 0,
@@ -1029,6 +1071,31 @@ struct isl_mat *isl_mat_drop_rows(struct isl_mat *mat, unsigned row, unsigned n)
        return mat;
 }
 
+__isl_give isl_mat *isl_mat_insert_cols(__isl_take isl_mat *mat,
+                               unsigned col, unsigned n)
+{
+       isl_mat *ext;
+
+       if (!mat)
+               return NULL;
+       if (n == 0)
+               return mat;
+
+       ext = isl_mat_alloc(mat->ctx, mat->n_row, mat->n_col + n);
+       if (!ext)
+               goto error;
+
+       isl_mat_sub_copy(mat->ctx, ext->row, mat->row, mat->n_row, 0, 0, col);
+       isl_mat_sub_copy(mat->ctx, ext->row, mat->row, mat->n_row,
+                               col + n, col, mat->n_col - col);
+
+       isl_mat_free(mat);
+       return ext;
+error:
+       isl_mat_free(mat);
+       return NULL;
+}
+
 void isl_mat_col_submul(struct isl_mat *mat,
                        int dst_col, isl_int f, int src_col)
 {
@@ -1051,14 +1118,17 @@ struct isl_mat *isl_mat_unimodular_complete(struct isl_mat *M, int row)
        int r;
        struct isl_mat *H = NULL, *Q = NULL;
 
-       isl_assert(ctx, M->n_row == M->n_col, goto error);
+       if (!M)
+               return NULL;
+
+       isl_assert(M->ctx, M->n_row == M->n_col, goto error);
        M->n_row = row;
        H = isl_mat_left_hermite(isl_mat_copy(M), 0, NULL, &Q);
        M->n_row = M->n_col;
        if (!H)
                goto error;
        for (r = 0; r < row; ++r)
-               isl_assert(ctx, isl_int_is_one(H->row[r][r]), goto error);
+               isl_assert(M->ctx, isl_int_is_one(H->row[r][r]), goto error);
        for (r = row; r < M->n_row; ++r)
                isl_seq_cpy(M->row[r], Q->row[r], M->n_col);
        isl_mat_free(H);
@@ -1070,3 +1140,125 @@ error:
        isl_mat_free(M);
        return NULL;
 }
+
+__isl_give isl_mat *isl_mat_concat(__isl_take isl_mat *top,
+       __isl_take isl_mat *bot)
+{
+       struct isl_mat *mat;
+
+       if (!top || !bot)
+               goto error;
+
+       isl_assert(top->ctx, top->n_col == bot->n_col, goto error);
+       if (top->n_row == 0) {
+               isl_mat_free(top);
+               return bot;
+       }
+       if (bot->n_row == 0) {
+               isl_mat_free(bot);
+               return top;
+       }
+
+       mat = isl_mat_alloc(top->ctx, top->n_row + bot->n_row, top->n_col);
+       if (!mat)
+               goto error;
+       isl_mat_sub_copy(mat->ctx, mat->row, top->row, top->n_row,
+                        0, 0, mat->n_col);
+       isl_mat_sub_copy(mat->ctx, mat->row + top->n_row, bot->row, bot->n_row,
+                        0, 0, mat->n_col);
+       isl_mat_free(top);
+       isl_mat_free(bot);
+       return mat;
+error:
+       isl_mat_free(top);
+       isl_mat_free(bot);
+       return NULL;
+}
+
+int isl_mat_is_equal(__isl_keep isl_mat *mat1, __isl_keep isl_mat *mat2)
+{
+       int i;
+
+       if (!mat1 || !mat2)
+               return -1;
+
+       if (mat1->n_row != mat2->n_row)
+               return 0;
+
+       if (mat1->n_col != mat2->n_col)
+               return 0;
+
+       for (i = 0; i < mat1->n_row; ++i)
+               if (!isl_seq_eq(mat1->row[i], mat2->row[i], mat1->n_col))
+                       return 0;
+
+       return 1;
+}
+
+__isl_give isl_mat *isl_mat_from_row_vec(__isl_take isl_vec *vec)
+{
+       struct isl_mat *mat;
+
+       if (!vec)
+               return NULL;
+       mat = isl_mat_alloc(vec->ctx, 1, vec->size);
+       if (!mat)
+               goto error;
+
+       isl_seq_cpy(mat->row[0], vec->el, vec->size);
+
+       isl_vec_free(vec);
+       return mat;
+error:
+       isl_vec_free(vec);
+       return NULL;
+}
+
+__isl_give isl_mat *isl_mat_vec_concat(__isl_take isl_mat *top,
+       __isl_take isl_vec *bot)
+{
+       return isl_mat_concat(top, isl_mat_from_row_vec(bot));
+}
+
+__isl_give isl_mat *isl_mat_move_cols(__isl_take isl_mat *mat,
+       unsigned dst_col, unsigned src_col, unsigned n)
+{
+       isl_mat *res;
+
+       if (!mat)
+               return NULL;
+       if (n == 0 || dst_col == src_col)
+               return mat;
+
+       res = isl_mat_alloc(mat->ctx, mat->n_row, mat->n_col);
+       if (!res)
+               goto error;
+
+       if (dst_col < src_col) {
+               isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
+                                0, 0, dst_col);
+               isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
+                                dst_col, src_col, n);
+               isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
+                                dst_col + n, dst_col, src_col - dst_col);
+               isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
+                                src_col + n, src_col + n,
+                                res->n_col - src_col - n);
+       } else {
+               isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
+                                0, 0, src_col);
+               isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
+                                src_col, src_col + n, dst_col - src_col);
+               isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
+                                dst_col, src_col, n);
+               isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
+                                dst_col + n, dst_col + n,
+                                res->n_col - dst_col - n);
+       }
+       isl_mat_free(mat);
+
+       return res;
+error:
+       isl_mat_free(mat);
+       return NULL;
+}