Skip to content

Commit

Permalink
vector stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrik-johansson committed Jan 16, 2023
1 parent 86f7522 commit 38e52f3
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 0 deletions.
2 changes: 2 additions & 0 deletions doc/source/gr_vec.rst
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,6 @@ Dot products
Other functions
--------------------------------------------------------------------------------

.. function:: int _gr_vec_step(gr_ptr vec, gr_srcptr start, gr_srcptr step, slong len, gr_ctx_t ctx)

.. function:: int _gr_vec_set_powers(gr_ptr res, gr_srcptr x, slong len, gr_ctx_t ctx)
7 changes: 7 additions & 0 deletions gr/matrix.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ truth_t gr_ctx_matrix_is_fixed_size(gr_ctx_t ctx)
return (MATRIX_CTX(ctx)->all_sizes) ? T_FALSE : T_TRUE;
}

truth_t
matrix_ctx_is_threadsafe(gr_ctx_t ctx)
{
return gr_ctx_is_threadsafe(MATRIX_CTX(ctx)->base_ring);
}

void
matrix_clear(gr_mat_t res, gr_ctx_t ctx)
{
Expand Down Expand Up @@ -462,6 +468,7 @@ gr_method_tab_input _gr_mat_methods_input[] =
{
{GR_METHOD_CTX_WRITE, (gr_funcptr) matrix_ctx_write},
{GR_METHOD_CTX_IS_RING, (gr_funcptr) matrix_ctx_is_ring},
{GR_METHOD_CTX_IS_THREADSAFE, (gr_funcptr) matrix_ctx_is_threadsafe},
{GR_METHOD_INIT, (gr_funcptr) matrix_init},
{GR_METHOD_CLEAR, (gr_funcptr) matrix_clear},
{GR_METHOD_SWAP, (gr_funcptr) matrix_swap},
Expand Down
7 changes: 7 additions & 0 deletions gr/mpoly.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ mpolynomial_ctx_is_field(gr_ctx_t ctx)
return T_FALSE;
}

truth_t
mpolynomial_ctx_is_threadsafe(gr_ctx_t ctx)
{
return gr_ctx_is_threadsafe(MPOLYNOMIAL_ELEM_CTX(ctx));
}

void
mpolynomial_init(gr_mpoly_t res, gr_ctx_t ctx)
{
Expand Down Expand Up @@ -206,6 +212,7 @@ gr_method_tab_input _gr_mpolynomial_methods_input[] =
{GR_METHOD_CTX_IS_COMMUTATIVE_RING, (gr_funcptr) mpolynomial_ctx_is_commutative_ring},
{GR_METHOD_CTX_IS_INTEGRAL_DOMAIN, (gr_funcptr) mpolynomial_ctx_is_integral_domain},
{GR_METHOD_CTX_IS_FIELD, (gr_funcptr) mpolynomial_ctx_is_field},
{GR_METHOD_CTX_IS_THREADSAFE, (gr_funcptr) mpolynomial_ctx_is_threadsafe},
{GR_METHOD_INIT, (gr_funcptr) mpolynomial_init},
{GR_METHOD_CLEAR, (gr_funcptr) mpolynomial_clear},
{GR_METHOD_SWAP, (gr_funcptr) mpolynomial_swap},
Expand Down
8 changes: 8 additions & 0 deletions gr/polynomial.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ polynomial_ctx_is_integral_domain(gr_ctx_t ctx)
return gr_ctx_is_integral_domain(POLYNOMIAL_ELEM_CTX(ctx));
}

truth_t
polynomial_ctx_is_threadsafe(gr_ctx_t ctx)
{
return gr_ctx_is_threadsafe(POLYNOMIAL_ELEM_CTX(ctx));
}


void
polynomial_clear(gr_poly_t res, gr_ctx_t ctx)
{
Expand Down Expand Up @@ -237,6 +244,7 @@ gr_method_tab_input _gr_poly_methods_input[] =
{GR_METHOD_CTX_IS_COMMUTATIVE_RING, (gr_funcptr) polynomial_ctx_is_commutative_ring},
{GR_METHOD_CTX_IS_INTEGRAL_DOMAIN, (gr_funcptr) polynomial_ctx_is_integral_domain},
{GR_METHOD_CTX_IS_FIELD, (gr_funcptr) gr_generic_ctx_predicate_false},
{GR_METHOD_CTX_IS_THREADSAFE, (gr_funcptr) polynomial_ctx_is_threadsafe},

{GR_METHOD_INIT, (gr_funcptr) polynomial_init},
{GR_METHOD_CLEAR, (gr_funcptr) polynomial_clear},
Expand Down
8 changes: 8 additions & 0 deletions gr/vector.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ truth_t gr_ctx_vector_gr_vec_is_fixed_size(gr_ctx_t ctx)
return (VECTOR_CTX(ctx)->all_sizes) ? T_FALSE : T_TRUE;
}

truth_t
vector_ctx_is_threadsafe(gr_ctx_t ctx)
{
return gr_ctx_is_threadsafe(ENTRY_CTX(ctx));
}

void
vector_gr_vec_clear(gr_vec_t res, gr_ctx_t ctx)
{
Expand Down Expand Up @@ -338,6 +344,8 @@ gr_static_method_table _gr_vec_methods;

gr_method_tab_input _gr_vec_methods_input[] =
{
{GR_METHOD_CTX_IS_THREADSAFE, (gr_funcptr) vector_ctx_is_threadsafe},

{GR_METHOD_CTX_WRITE, (gr_funcptr) vector_gr_vec_ctx_write},
{GR_METHOD_INIT, (gr_funcptr) vector_gr_vec_init},
{GR_METHOD_CLEAR, (gr_funcptr) vector_gr_vec_clear},
Expand Down
1 change: 1 addition & 0 deletions gr_vec.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ WARN_UNUSED_RESULT int _gr_vec_product_parallel(gr_ptr res, gr_srcptr vec, slong
WARN_UNUSED_RESULT int _gr_vec_product_serial(gr_ptr res, gr_srcptr vec, slong len, gr_ctx_t ctx);
WARN_UNUSED_RESULT int _gr_vec_product_generic(gr_ptr res, gr_srcptr vec, slong len, gr_ctx_t ctx);

WARN_UNUSED_RESULT int _gr_vec_step(gr_ptr vec, gr_srcptr start, gr_srcptr step, slong len, gr_ctx_t ctx);

#ifdef __cplusplus
}
Expand Down
29 changes: 29 additions & 0 deletions gr_vec/step.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
Copyright (C) 2023 Fredrik Johansson
This file is part of FLINT.
FLINT 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 "gr_vec.h"

/* todo: with floats, prefer multiplying to adding */
int _gr_vec_step(gr_ptr vec, gr_srcptr start, gr_srcptr step, slong len, gr_ctx_t ctx)
{
gr_method_binary_op add = GR_BINARY_OP(ctx, ADD);
int status = GR_SUCCESS;
slong i, sz = ctx->sizeof_elem;;

if (len <= 0)
return GR_SUCCESS;

status |= gr_set(vec, start, ctx);
for (i = 1; i < len; i++)
status |= add(GR_ENTRY(vec, i, sz), GR_ENTRY(vec, i - 1, sz), step, ctx);

return status;
}
15 changes: 15 additions & 0 deletions python/flint_ctypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2817,6 +2817,10 @@ class gr_vec(gr_elem):
_struct_type = gr_vec_struct

def __init__(self, *args, **kwargs):
"""
>>> VecZZ(range(3, 20, 3))
[3, 6, 9, 12, 15, 18]
"""
context = kwargs['context']
gr_elem.__init__(self, None, context)
element_ring = context._element_ring
Expand All @@ -2838,6 +2842,17 @@ def __init__(self, *args, **kwargs):
status |= libgr.gr_set(iptr, x._ref, x._ctx)
elif isinstance(val, gr_elem):
status = libgr.gr_set_other(self._ref, val._ref, val._ctx, self._ctx)
elif isinstance(val, range):
start = val.start
step = val.step
n = len(val)
# todo: watch for slong -> int
status = libgr._gr_vec_check_resize(self._ref, n, self._ctx)
if not status:
start = element_ring(start)
step = element_ring(step)
iptr = libgr.gr_vec_entry_ptr(self._ref, 0, element_ring._ref)
status = libgr._gr_vec_step(iptr, start._ref, step._ref, n, element_ring._ref)
if status:
if status & GR_UNABLE: raise NotImplementedError
if status & GR_DOMAIN: raise ValueError
Expand Down

0 comments on commit 38e52f3

Please sign in to comment.