Skip to content

Commit 8db49c9

Browse files
Merge pull request #2623 from fredrik-johansson/noncom
Improve support for generic noncommutative rings
2 parents 917b9c2 + 738f982 commit 8db49c9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+221
-117
lines changed

doc/source/gr_domains.rst

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,23 @@ Domain properties
3535
mathematical property.
3636
The result can be ``T_UNKNOWN``.
3737

38+
.. function:: truth_t gr_ctx_is_approx_commutative_ring(gr_ctx_t ctx)
39+
40+
Returns whether the structure `\tilde R` implemented by ``ctx`` is assumed
41+
to represent an approximate version of a structure `R` with the
42+
respective mathematical property.
43+
The usual axioms need not hold exactly as long as they hold
44+
within a small perturbation, i.e. an axiom of the type `a = b` in `R` is
45+
replaced by an axiom of the type
46+
`|\tilde a - \tilde b| < \varepsilon` in `\tilde R`.
47+
For example, polynomials with floating-point coefficients are
48+
considered an approximate commutative ring, but two by two matrices
49+
with floating-point coefficients are not.
50+
51+
The exact structure is trivially considered an approximate version of
52+
itself; for example, every commutative ring is considered to be an
53+
approximate commutative ring.
54+
3855
.. function:: truth_t gr_ctx_is_exact(gr_ctx_t ctx)
3956

4057
Returns whether the representation of elements is always exact.
@@ -109,6 +126,8 @@ Basic rings and fields
109126
-------------------------------------------------------------------------------
110127

111128
.. function:: void gr_ctx_init_random(gr_ctx_t ctx, flint_rand_t state)
129+
void gr_ctx_init_random_commutative_ring(gr_ctx_t ctx, flint_rand_t state)
130+
void gr_ctx_init_random_field(gr_ctx_t ctx, flint_rand_t state)
112131

113132
Initializes *ctx* to a random ring. This will currently
114133
only generate base rings and composite rings over certain

src/gr.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ typedef enum
117117
GR_METHOD_CTX_IS_COMPLEX_VECTOR_SPACE,
118118
GR_METHOD_CTX_IS_ORDERED_RING,
119119

120+
GR_METHOD_CTX_IS_APPROX_COMMUTATIVE_RING,
121+
120122
/* group properties */
121123
GR_METHOD_CTX_IS_MULTIPLICATIVE_GROUP,
122124

@@ -972,13 +974,14 @@ GR_INLINE truth_t gr_ctx_is_zero_ring(gr_ctx_t ctx) { return GR_CTX_PREDICATE(ct
972974
GR_INLINE truth_t gr_ctx_is_rational_vector_space(gr_ctx_t ctx) { return GR_CTX_PREDICATE(ctx, CTX_IS_RATIONAL_VECTOR_SPACE)(ctx); }
973975
GR_INLINE truth_t gr_ctx_is_real_vector_space(gr_ctx_t ctx) { return GR_CTX_PREDICATE(ctx, CTX_IS_REAL_VECTOR_SPACE)(ctx); }
974976
GR_INLINE truth_t gr_ctx_is_complex_vector_space(gr_ctx_t ctx) { return GR_CTX_PREDICATE(ctx, CTX_IS_COMPLEX_VECTOR_SPACE)(ctx); }
975-
976977
GR_INLINE truth_t gr_ctx_is_unique_factorization_domain(gr_ctx_t ctx) { return GR_CTX_PREDICATE(ctx, CTX_IS_UNIQUE_FACTORIZATION_DOMAIN)(ctx); }
977978
GR_INLINE truth_t gr_ctx_is_finite(gr_ctx_t ctx) { return GR_CTX_PREDICATE(ctx, CTX_IS_FINITE)(ctx); }
978979
GR_INLINE truth_t gr_ctx_is_finite_characteristic(gr_ctx_t ctx) { return GR_CTX_PREDICATE(ctx, CTX_IS_FINITE_CHARACTERISTIC)(ctx); }
979980
GR_INLINE truth_t gr_ctx_is_algebraically_closed(gr_ctx_t ctx) { return GR_CTX_PREDICATE(ctx, CTX_IS_ALGEBRAICALLY_CLOSED)(ctx); }
980981
GR_INLINE truth_t gr_ctx_is_ordered_ring(gr_ctx_t ctx) { return GR_CTX_PREDICATE(ctx, CTX_IS_ORDERED_RING)(ctx); }
981982

983+
GR_INLINE truth_t gr_ctx_is_approx_commutative_ring(gr_ctx_t ctx) { return GR_CTX_PREDICATE(ctx, CTX_IS_APPROX_COMMUTATIVE_RING)(ctx); }
984+
982985
GR_INLINE truth_t gr_ctx_is_multiplicative_group(gr_ctx_t ctx) { return GR_CTX_PREDICATE(ctx, CTX_IS_MULTIPLICATIVE_GROUP)(ctx); }
983986

984987
GR_INLINE truth_t gr_ctx_is_exact(gr_ctx_t ctx) { return GR_CTX_PREDICATE(ctx, CTX_IS_EXACT)(ctx); }
@@ -1384,6 +1387,8 @@ truth_t gr_generic_ctx_predicate_false(gr_ctx_t ctx);
13841387
void gr_ctx_uninitialized(gr_ctx_t ctx);
13851388

13861389
void gr_ctx_init_random(gr_ctx_t ctx, flint_rand_t state);
1390+
void gr_ctx_init_random_commutative_ring(gr_ctx_t ctx, flint_rand_t state);
1391+
void gr_ctx_init_random_field(gr_ctx_t ctx, flint_rand_t state);
13871392

13881393
void gr_ctx_init_fmpz(gr_ctx_t ctx);
13891394
void gr_ctx_init_fmpq(gr_ctx_t ctx);

src/gr/acf.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1317,6 +1317,7 @@ gr_method_tab_input _acf_methods_input[] =
13171317
(gr_funcptr) gr_generic_ctx_predicate_false},
13181318
{GR_METHOD_CTX_IS_ORDERED_RING,
13191319
(gr_funcptr) gr_generic_ctx_predicate_false},
1320+
{GR_METHOD_CTX_IS_APPROX_COMMUTATIVE_RING, (gr_funcptr) gr_generic_ctx_predicate_true},
13201321
{GR_METHOD_CTX_IS_EXACT, (gr_funcptr) gr_generic_ctx_predicate_false},
13211322
{GR_METHOD_CTX_IS_CANONICAL,
13221323
(gr_funcptr) gr_generic_ctx_predicate_false},

src/gr/arf.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1349,6 +1349,7 @@ gr_method_tab_input _arf_methods_input[] =
13491349
(gr_funcptr) gr_generic_ctx_predicate_false},
13501350
{GR_METHOD_CTX_IS_ORDERED_RING,
13511351
(gr_funcptr) gr_generic_ctx_predicate_false},
1352+
{GR_METHOD_CTX_IS_APPROX_COMMUTATIVE_RING, (gr_funcptr) gr_generic_ctx_predicate_true},
13521353
{GR_METHOD_CTX_IS_EXACT, (gr_funcptr) gr_generic_ctx_predicate_false},
13531354
{GR_METHOD_CTX_IS_CANONICAL,
13541355
(gr_funcptr) gr_generic_ctx_predicate_false},

src/gr/complex.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ static truth_t _gr_complex_ctx_is_integral_domain(gr_ctx_t ctx) { return gr_ctx_
5454
static truth_t _gr_complex_ctx_is_field(gr_ctx_t ctx) { return gr_ctx_is_field(REAL_CTX(ctx)); }
5555
static truth_t _gr_complex_ctx_is_rational_vector_space(gr_ctx_t ctx) { return gr_ctx_is_rational_vector_space(REAL_CTX(ctx)); }
5656
static truth_t _gr_complex_ctx_is_real_vector_space(gr_ctx_t ctx) { return gr_ctx_is_real_vector_space(REAL_CTX(ctx)); }
57+
static truth_t _gr_complex_ctx_is_approx_commutative_ring(gr_ctx_t ctx) { return gr_ctx_is_approx_commutative_ring(REAL_CTX(ctx)); }
5758

5859
static truth_t _gr_complex_ctx_is_threadsafe(gr_ctx_t ctx) { return gr_ctx_is_threadsafe(REAL_CTX(ctx)); }
5960
static truth_t _gr_complex_ctx_is_finite(gr_ctx_t ctx) { return gr_ctx_is_finite(REAL_CTX(ctx)); }
@@ -633,6 +634,7 @@ gr_method_tab_input _gr_complex_methods_input[] =
633634
{GR_METHOD_CTX_IS_THREADSAFE, (gr_funcptr) _gr_complex_ctx_is_threadsafe},
634635
{GR_METHOD_CTX_IS_FINITE, (gr_funcptr) _gr_complex_ctx_is_finite},
635636
{GR_METHOD_CTX_IS_FINITE_CHARACTERISTIC, (gr_funcptr) _gr_complex_ctx_is_finite_characteristic},
637+
{GR_METHOD_CTX_IS_APPROX_COMMUTATIVE_RING, (gr_funcptr) _gr_complex_ctx_is_approx_commutative_ring},
636638

637639
{GR_METHOD_CTX_IS_EXACT, (gr_funcptr) _gr_complex_ctx_is_exact},
638640
{GR_METHOD_CTX_NGENS, (gr_funcptr) gr_generic_ctx_ngens_1},

src/gr/init_random.c

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ gr_ctx_init_random_ring_composite(gr_ctx_t ctx, flint_rand_t state)
4848
{
4949
gr_ctx_struct * base_ring = _gr_random_base_ring(state);
5050

51-
switch (n_randint(state, 6))
51+
switch (n_randint(state, 10))
5252
{
5353
case 0:
5454
gr_ctx_init_gr_poly(ctx, base_ring);
@@ -74,13 +74,11 @@ gr_ctx_init_random_ring_composite(gr_ctx_t ctx, flint_rand_t state)
7474
gr_ctx_init_debug(ctx, base_ring, flags, unable_probability);
7575
}
7676
break;
77-
/*
78-
this will break tests that currently assume commutativity
79-
80-
case 5:
77+
/* generate with slightly higher probability, because this is currently
78+
the only way we generate noncommutative test rings */
79+
default:
8180
gr_ctx_init_matrix_ring(ctx, base_ring, n_randint(state, 4));
8281
break;
83-
*/
8482
}
8583
}
8684

@@ -277,3 +275,25 @@ void gr_ctx_init_random(gr_ctx_t ctx, flint_rand_t state)
277275
gr_ctx_println(ctx);
278276
*/
279277
}
278+
279+
void gr_ctx_init_random_commutative_ring(gr_ctx_t ctx, flint_rand_t state)
280+
{
281+
while (1)
282+
{
283+
gr_ctx_init_random(ctx, state);
284+
if (gr_ctx_is_commutative_ring(ctx) == T_TRUE)
285+
break;
286+
gr_ctx_clear(ctx);
287+
}
288+
}
289+
290+
void gr_ctx_init_random_field(gr_ctx_t ctx, flint_rand_t state)
291+
{
292+
while (1)
293+
{
294+
gr_ctx_init_random(ctx, state);
295+
if (gr_ctx_is_field(ctx) == T_TRUE)
296+
break;
297+
gr_ctx_clear(ctx);
298+
}
299+
}

src/gr/matrix.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,25 @@ static truth_t matrix_ctx_is_complex_vector_space(gr_ctx_t ctx)
136136
return gr_ctx_is_complex_vector_space(MATRIX_CTX(ctx)->base_ring);
137137
}
138138

139+
static truth_t matrix_ctx_is_approx_commutative_ring(gr_ctx_t ctx)
140+
{
141+
int shape_ok = (!MATRIX_CTX(ctx)->all_sizes && MATRIX_CTX(ctx)->nrows == MATRIX_CTX(ctx)->ncols);
142+
143+
if (!shape_ok)
144+
return T_FALSE;
145+
146+
if (MATRIX_CTX(ctx)->nrows == 0)
147+
return T_TRUE;
148+
149+
if (MATRIX_CTX(ctx)->nrows == 1)
150+
return gr_ctx_is_approx_commutative_ring(MATRIX_CTX(ctx)->base_ring);
151+
152+
if (gr_ctx_is_zero_ring(MATRIX_CTX(ctx)->base_ring) == T_TRUE)
153+
return T_TRUE;
154+
155+
return T_UNKNOWN;
156+
}
157+
139158
/* todo: public */
140159
truth_t gr_ctx_matrix_is_fixed_size(gr_ctx_t ctx)
141160
{
@@ -580,6 +599,7 @@ gr_method_tab_input _gr_mat_methods_input[] =
580599
{GR_METHOD_CTX_IS_RATIONAL_VECTOR_SPACE, (gr_funcptr) matrix_ctx_is_rational_vector_space},
581600
{GR_METHOD_CTX_IS_REAL_VECTOR_SPACE, (gr_funcptr) matrix_ctx_is_real_vector_space},
582601
{GR_METHOD_CTX_IS_COMPLEX_VECTOR_SPACE, (gr_funcptr) matrix_ctx_is_complex_vector_space},
602+
{GR_METHOD_CTX_IS_APPROX_COMMUTATIVE_RING, (gr_funcptr) matrix_ctx_is_approx_commutative_ring},
583603
{GR_METHOD_INIT, (gr_funcptr) matrix_init},
584604
{GR_METHOD_CLEAR, (gr_funcptr) matrix_clear},
585605
{GR_METHOD_SWAP, (gr_funcptr) matrix_swap},

src/gr/mpf.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ gr_method_tab_input _gr_mpf_methods_input[] =
370370
{GR_METHOD_CTX_WRITE, (gr_funcptr) _gr_mpf_ctx_write},
371371
{GR_METHOD_CTX_IS_RING, (gr_funcptr) gr_generic_ctx_predicate_false},
372372
{GR_METHOD_CTX_IS_EXACT, (gr_funcptr) gr_generic_ctx_predicate_false},
373+
{GR_METHOD_CTX_IS_APPROX_COMMUTATIVE_RING, (gr_funcptr) gr_generic_ctx_predicate_true},
373374
{GR_METHOD_CTX_HAS_REAL_PREC, (gr_funcptr) gr_generic_ctx_predicate_true},
374375
{GR_METHOD_CTX_SET_REAL_PREC, (gr_funcptr) _gr_mpf_ctx_set_real_prec},
375376
{GR_METHOD_CTX_GET_REAL_PREC, (gr_funcptr) _gr_mpf_ctx_get_real_prec},

src/gr/polynomial.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,12 @@ polynomial_ctx_is_complex_vector_space(gr_ctx_t ctx)
121121
return gr_ctx_is_complex_vector_space(POLYNOMIAL_ELEM_CTX(ctx));
122122
}
123123

124+
static truth_t
125+
polynomial_ctx_is_approx_commutative_ring(gr_ctx_t ctx)
126+
{
127+
return gr_ctx_is_approx_commutative_ring(POLYNOMIAL_ELEM_CTX(ctx));
128+
}
129+
124130
static truth_t
125131
polynomial_ctx_is_threadsafe(gr_ctx_t ctx)
126132
{
@@ -770,6 +776,7 @@ gr_method_tab_input _gr_poly_methods_input[] =
770776
{GR_METHOD_CTX_IS_RATIONAL_VECTOR_SPACE, (gr_funcptr) polynomial_ctx_is_rational_vector_space},
771777
{GR_METHOD_CTX_IS_REAL_VECTOR_SPACE, (gr_funcptr) polynomial_ctx_is_real_vector_space},
772778
{GR_METHOD_CTX_IS_COMPLEX_VECTOR_SPACE, (gr_funcptr) polynomial_ctx_is_complex_vector_space},
779+
{GR_METHOD_CTX_IS_APPROX_COMMUTATIVE_RING, (gr_funcptr) polynomial_ctx_is_approx_commutative_ring},
773780
{GR_METHOD_CTX_IS_THREADSAFE, (gr_funcptr) polynomial_ctx_is_threadsafe},
774781
{GR_METHOD_CTX_SET_GEN_NAME, (gr_funcptr) _gr_gr_poly_ctx_set_gen_name},
775782
{GR_METHOD_CTX_SET_GEN_NAMES, (gr_funcptr) _gr_gr_poly_ctx_set_gen_names},

src/gr/test/t-fmpz_mod_mpoly_q.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ TEST_FUNCTION_START(gr_fmpz_mod_mpoly_q, state)
3030

3131
gr_ctx_init_fmpz_mod_mpoly_q(ZZxy, n_randint(state, 3), mpoly_ordering_randtest(state), m);
3232
ZZxy->size_limit = 100;
33-
gr_test_ring(ZZxy, 1000, flags);
33+
gr_test_ring(ZZxy, 100, flags);
3434
gr_ctx_clear(ZZxy);
3535

3636
fmpz_clear(m);

0 commit comments

Comments
 (0)