Skip to content

Commit 86ca0b2

Browse files
fix bugs and test code
1 parent c4d92fb commit 86ca0b2

File tree

2 files changed

+38
-29
lines changed

2 files changed

+38
-29
lines changed

src/gr_mat/sub_scalar.c

+7-15
Original file line numberDiff line numberDiff line change
@@ -113,23 +113,15 @@ gr_mat_scalar_other_sub(gr_mat_t res, gr_srcptr x, gr_ctx_t x_ctx, const gr_mat_
113113
r = gr_mat_nrows(res, ctx);
114114
c = gr_mat_ncols(res, ctx);
115115

116-
if (res == mat)
117-
{
118-
for (i = 0; i < FLINT_MIN(r, c); i++)
119-
status |= gr_other_sub(GR_MAT_ENTRY(res, i, i, sz), x, x_ctx, GR_MAT_ENTRY(res, i, i, sz), ctx);
120-
}
121-
else
116+
for (i = 0; i < r; i++)
122117
{
123-
for (i = 0; i < r; i++)
118+
for (j = 0; j < c; j++)
124119
{
125-
for (j = 0; j < c; j++)
126-
{
127-
/* todo: vectorize */
128-
if (i == j)
129-
status |= gr_other_sub(GR_MAT_ENTRY(res, i, j, sz), x, x_ctx, GR_MAT_ENTRY(mat, i, j, sz), ctx);
130-
else
131-
status |= gr_set(GR_MAT_ENTRY(res, i, j, sz), GR_MAT_ENTRY(mat, i, j, sz), ctx);
132-
}
120+
/* todo: vectorize */
121+
if (i == j)
122+
status |= gr_other_sub(GR_MAT_ENTRY(res, i, j, sz), x, x_ctx, GR_MAT_ENTRY(mat, i, j, sz), ctx);
123+
else
124+
status |= gr_neg(GR_MAT_ENTRY(res, i, j, sz), GR_MAT_ENTRY(mat, i, j, sz), ctx);
133125
}
134126
}
135127

src/gr_mat/test/t-scalar.c

+31-14
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ TEST_FUNCTION_START(gr_mat_scalar, state)
5858
d = gr_heap_init(ctx);
5959
c_other = gr_heap_init(ctx_other);
6060

61+
GR_MUST_SUCCEED(gr_mat_randtest(A, state, ctx));
6162
GR_MUST_SUCCEED(gr_randtest(c, state, ctx));
6263

6364
have_other = (gr_set_other(c_other, c, ctx, ctx_other) == GR_SUCCESS);
@@ -70,62 +71,78 @@ TEST_FUNCTION_START(gr_mat_scalar, state)
7071
{
7172
status = GR_SUCCESS;
7273

74+
gr_mat_struct * A_or_B2_alias;
75+
gr_mat_struct * A_or_B3_alias;
76+
77+
if (n_randint(state, 2))
78+
{
79+
A_or_B2_alias = A;
80+
A_or_B3_alias = A;
81+
}
82+
else
83+
{
84+
status |= gr_mat_set(B2, A, ctx);
85+
status |= gr_mat_set(B3, A, ctx);
86+
A_or_B2_alias = B2;
87+
A_or_B3_alias = B3;
88+
}
89+
7390
if (testcase == 0)
7491
{
7592
/* A + c == A + C */
7693
status |= gr_mat_add(B1, A, Cmn, ctx);
77-
status |= gr_mat_add_scalar(B2, A, c, ctx);
94+
status |= gr_mat_add_scalar(B2, A_or_B2_alias, c, ctx);
7895
if (have_other)
79-
status |= gr_mat_add_scalar_other(B3, A, c_other, ctx_other, ctx);
96+
status |= gr_mat_add_scalar_other(B3, A_or_B3_alias, c_other, ctx_other, ctx);
8097
}
8198
else if (testcase == 1)
8299
{
83100
/* c + A == C + A */
84101
status |= gr_mat_add(B1, Cmn, A, ctx);
85-
status |= gr_mat_scalar_add(B2, c, A, ctx);
102+
status |= gr_mat_scalar_add(B2, c, A_or_B2_alias, ctx);
86103
if (have_other)
87-
status |= gr_mat_scalar_other_add(B3, c_other, ctx_other, A, ctx);
104+
status |= gr_mat_scalar_other_add(B3, c_other, ctx_other, A_or_B3_alias, ctx);
88105
}
89106
else if (testcase == 2)
90107
{
91108
/* A - c == A - C */
92109
status |= gr_mat_sub(B1, A, Cmn, ctx);
93-
status |= gr_mat_sub_scalar(B2, A, c, ctx);
110+
status |= gr_mat_sub_scalar(B2, A_or_B2_alias, c, ctx);
94111
if (have_other)
95-
status |= gr_mat_sub_scalar_other(B3, A, c_other, ctx_other, ctx);
112+
status |= gr_mat_sub_scalar_other(B3, A_or_B3_alias, c_other, ctx_other, ctx);
96113
}
97114
else if (testcase == 3)
98115
{
99116
/* c - A == C - A */
100117
status |= gr_mat_sub(B1, Cmn, A, ctx);
101-
status |= gr_mat_scalar_sub(B2, c, A, ctx);
118+
status |= gr_mat_scalar_sub(B2, c, A_or_B2_alias, ctx);
102119
if (have_other)
103-
status |= gr_mat_scalar_other_sub(B3, c_other, ctx_other, A, ctx);
120+
status |= gr_mat_scalar_other_sub(B3, c_other, ctx_other, A_or_B3_alias, ctx);
104121
}
105122
else if (testcase == 4)
106123
{
107124
/* A * c == A * C */
108125
status |= gr_mat_mul(B1, A, Cnn, ctx);
109-
status |= gr_mat_mul_scalar(B2, A, c, ctx);
126+
status |= gr_mat_mul_scalar(B2, A_or_B2_alias, c, ctx);
110127
if (have_other)
111-
status |= gr_mat_mul_scalar_other(B3, A, c_other, ctx_other, ctx);
128+
status |= gr_mat_mul_scalar_other(B3, A_or_B3_alias, c_other, ctx_other, ctx);
112129
}
113130
else if (testcase == 5)
114131
{
115132
/* A * c == A * C */
116133
status |= gr_mat_mul(B1, Cmm, A, ctx);
117-
status |= gr_mat_scalar_mul(B2, c, A, ctx);
134+
status |= gr_mat_scalar_mul(B2, c, A_or_B2_alias, ctx);
118135
if (have_other)
119-
status |= gr_mat_scalar_other_mul(B3, c_other, ctx_other, A, ctx);
136+
status |= gr_mat_scalar_other_mul(B3, c_other, ctx_other, A_or_B3_alias, ctx);
120137
}
121138
else if (testcase == 6)
122139
{
123140
/* A / c == A * c^(-1) */
124141
status |= gr_inv(d, c, ctx);
125142
status |= gr_mat_mul_scalar(B1, A, d, ctx);
126-
status |= gr_mat_div_scalar(B2, A, c, ctx);
143+
status |= gr_mat_div_scalar(B2, A_or_B2_alias, c, ctx);
127144
if (have_other)
128-
status |= gr_mat_div_scalar_other(B3, A, c_other, ctx_other, ctx);
145+
status |= gr_mat_div_scalar_other(B3, A_or_B3_alias, c_other, ctx_other, ctx);
129146
}
130147

131148
if (status == GR_SUCCESS && gr_mat_equal(B1, B2, ctx) == T_FALSE)

0 commit comments

Comments
 (0)