Skip to content

Commit

Permalink
Merge pull request #2012 from fredrik-johansson/nfloat11
Browse files Browse the repository at this point in the history
nfloat_complex: cmp, cmpabs, mul_2exp_si, abs
  • Loading branch information
fredrik-johansson authored Jun 5, 2024
2 parents f8d1a5f + 60540bb commit 187b9bf
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 5 deletions.
4 changes: 4 additions & 0 deletions doc/source/nfloat.rst
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,10 @@ real pairs.
int nfloat_complex_mul(nfloat_complex_ptr res, nfloat_complex_srcptr x, nfloat_complex_srcptr y, gr_ctx_t ctx)
int nfloat_complex_inv(nfloat_complex_ptr res, nfloat_complex_srcptr x, gr_ctx_t ctx)
int nfloat_complex_div(nfloat_complex_ptr res, nfloat_complex_srcptr x, nfloat_complex_srcptr y, gr_ctx_t ctx)
int nfloat_complex_mul_2exp_si(nfloat_complex_ptr res, nfloat_complex_srcptr x, slong y, gr_ctx_t ctx)
int nfloat_complex_cmp(int * res, nfloat_complex_srcptr x, nfloat_complex_srcptr y, gr_ctx_t ctx)
int nfloat_complex_cmpabs(int * res, nfloat_complex_srcptr x, nfloat_complex_srcptr y, gr_ctx_t ctx)
int nfloat_complex_abs(nfloat_ptr res, nfloat_complex_srcptr x, gr_ctx_t ctx)
void _nfloat_complex_vec_init(nfloat_complex_ptr res, slong len, gr_ctx_t ctx)
void _nfloat_complex_vec_clear(nfloat_complex_ptr res, slong len, gr_ctx_t ctx)
int _nfloat_complex_vec_zero(nfloat_complex_ptr res, slong len, gr_ctx_t ctx)
Expand Down
4 changes: 4 additions & 0 deletions src/nfloat.h
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,10 @@ int _nfloat_complex_mul_karatsuba(nfloat_ptr res1, nfloat_ptr res2, nfloat_srcpt
int nfloat_complex_mul(nfloat_complex_ptr res, nfloat_complex_srcptr x, nfloat_complex_srcptr y, gr_ctx_t ctx);
int nfloat_complex_inv(nfloat_complex_ptr res, nfloat_complex_srcptr x, gr_ctx_t ctx);
int nfloat_complex_div(nfloat_complex_ptr res, nfloat_complex_srcptr x, nfloat_complex_srcptr y, gr_ctx_t ctx);
int nfloat_complex_mul_2exp_si(nfloat_complex_ptr res, nfloat_complex_srcptr x, slong y, gr_ctx_t ctx);
int nfloat_complex_cmp(int * res, nfloat_complex_srcptr x, nfloat_complex_srcptr y, gr_ctx_t ctx);
int nfloat_complex_cmpabs(int * res, nfloat_complex_srcptr x, nfloat_complex_srcptr y, gr_ctx_t ctx);
int nfloat_complex_abs(nfloat_ptr res, nfloat_complex_srcptr x, gr_ctx_t ctx);

void _nfloat_complex_vec_init(nfloat_complex_ptr res, slong len, gr_ctx_t ctx);
void _nfloat_complex_vec_clear(nfloat_complex_ptr res, slong len, gr_ctx_t ctx);
Expand Down
74 changes: 69 additions & 5 deletions src/nfloat/complex.c
Original file line number Diff line number Diff line change
Expand Up @@ -1460,6 +1460,71 @@ nfloat_complex_div(nfloat_complex_ptr res, nfloat_complex_srcptr x, nfloat_compl
return status;
}

int
nfloat_complex_mul_2exp_si(nfloat_complex_ptr res, nfloat_complex_srcptr x, slong y, gr_ctx_t ctx)
{
int status = GR_SUCCESS;
status |= nfloat_mul_2exp_si(NFLOAT_COMPLEX_RE(res, ctx), NFLOAT_COMPLEX_RE(x, ctx), y, ctx);
status |= nfloat_mul_2exp_si(NFLOAT_COMPLEX_IM(res, ctx), NFLOAT_COMPLEX_IM(x, ctx), y, ctx);
return status;
}

int
nfloat_complex_cmp(int * res, nfloat_complex_srcptr x, nfloat_complex_srcptr y, gr_ctx_t ctx)
{
if (NFLOAT_CTX_HAS_INF_NAN(ctx))
return GR_UNABLE;

if (!NFLOAT_IS_ZERO(NFLOAT_COMPLEX_IM(x, ctx)) ||
!NFLOAT_IS_ZERO(NFLOAT_COMPLEX_IM(y, ctx)))
return GR_DOMAIN;

return nfloat_cmp(res, NFLOAT_COMPLEX_RE(x, ctx), NFLOAT_COMPLEX_RE(y, ctx), ctx);
}

int
nfloat_complex_cmpabs(int * res, nfloat_complex_srcptr x, nfloat_complex_srcptr y, gr_ctx_t ctx)
{
if (NFLOAT_CTX_HAS_INF_NAN(ctx))
return GR_UNABLE;

if (!NFLOAT_IS_ZERO(NFLOAT_COMPLEX_IM(x, ctx)) ||
!NFLOAT_IS_ZERO(NFLOAT_COMPLEX_IM(y, ctx)))
return GR_UNABLE;

return nfloat_cmpabs(res, NFLOAT_COMPLEX_RE(x, ctx), NFLOAT_COMPLEX_RE(y, ctx), ctx);
}

int
nfloat_complex_abs(nfloat_complex_ptr res, nfloat_complex_srcptr x, gr_ctx_t ctx)
{
ulong t[NFLOAT_MAX_ALLOC];
ulong u[NFLOAT_MAX_ALLOC];
int status = GR_SUCCESS;

if (NFLOAT_CTX_HAS_INF_NAN(ctx))
return GR_UNABLE;

if (NFLOAT_IS_ZERO(NFLOAT_COMPLEX_IM(x, ctx)))
{
status = nfloat_abs(NFLOAT_COMPLEX_RE(res, ctx), NFLOAT_COMPLEX_RE(x, ctx), ctx);
}
else if (NFLOAT_IS_ZERO(NFLOAT_COMPLEX_RE(x, ctx)))
{
status = nfloat_abs(NFLOAT_COMPLEX_RE(res, ctx), NFLOAT_COMPLEX_IM(x, ctx), ctx);
}
else
{
status |= nfloat_sqr(t, NFLOAT_COMPLEX_RE(x, ctx), ctx);
status |= nfloat_sqr(u, NFLOAT_COMPLEX_IM(x, ctx), ctx);
status |= nfloat_add(NFLOAT_COMPLEX_RE(res, ctx), t, u, ctx);
status |= nfloat_sqrt(NFLOAT_COMPLEX_RE(res, ctx), NFLOAT_COMPLEX_RE(res, ctx), ctx);
}

status |= nfloat_zero(NFLOAT_COMPLEX_IM(res, ctx), ctx);
return status;
}

void
_nfloat_complex_vec_init(nfloat_complex_ptr res, slong len, gr_ctx_t ctx)
{
Expand Down Expand Up @@ -1591,9 +1656,7 @@ gr_method_tab_input _nfloat_complex_methods_input[] =
{GR_METHOD_DIV_FMPZ, (gr_funcptr) nfloat_complex_div_fmpz},
*/
{GR_METHOD_INV, (gr_funcptr) nfloat_complex_inv},
/*
{GR_METHOD_MUL_2EXP_SI, (gr_funcptr) nfloat_complex_mul_2exp_si},
*/
{GR_METHOD_MUL_2EXP_SI, (gr_funcptr) nfloat_complex_mul_2exp_si},
/*
{GR_METHOD_MUL_2EXP_FMPZ, (gr_funcptr) nfloat_complex_mul_2exp_fmpz},
{GR_METHOD_SET_FMPZ_2EXP_FMPZ, (gr_funcptr) nfloat_complex_set_fmpz_2exp_fmpz},
Expand Down Expand Up @@ -1623,18 +1686,19 @@ gr_method_tab_input _nfloat_complex_methods_input[] =
{GR_METHOD_CEIL, (gr_funcptr) nfloat_complex_ceil},
{GR_METHOD_TRUNC, (gr_funcptr) nfloat_complex_trunc},
{GR_METHOD_NINT, (gr_funcptr) nfloat_complex_nint},
*/

{GR_METHOD_ABS, (gr_funcptr) nfloat_complex_abs},
*/
{GR_METHOD_CONJ, (gr_funcptr) nfloat_complex_set},
{GR_METHOD_RE, (gr_funcptr) nfloat_complex_set},
{GR_METHOD_IM, (gr_funcptr) nfloat_complex_im},
/*
{GR_METHOD_SGN, (gr_funcptr) nfloat_complex_sgn},
{GR_METHOD_CSGN, (gr_funcptr) nfloat_complex_sgn},
*/
{GR_METHOD_CMP, (gr_funcptr) nfloat_complex_cmp},
{GR_METHOD_CMPABS, (gr_funcptr) nfloat_complex_cmpabs},
*/

{GR_METHOD_I, (gr_funcptr) nfloat_complex_i},
{GR_METHOD_PI, (gr_funcptr) nfloat_complex_pi},
/*
Expand Down
4 changes: 4 additions & 0 deletions src/nfloat/test/t-nfloat_complex.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,17 @@ TEST_FUNCTION_START(nfloat_complex, state)
reps = (prec <= 256 ? 10000 : 1) * flint_test_multiplier();
for (iter = 0; iter < reps; iter++)
{
gr_test_cmp_fun(ctx, (gr_method_binary_op_get_int) gr_cmp, ctx2, state, 0);
gr_test_cmp_fun(ctx, (gr_method_binary_op_get_int) gr_cmpabs, ctx2, state, 0);
gr_test_approx_binary_op(ctx, (gr_method_binary_op) gr_add, ctx2, tol, state, 0);
gr_test_approx_binary_op(ctx, (gr_method_binary_op) gr_sub, ctx2, tol, state, 0);
gr_test_approx_binary_op(ctx, (gr_method_binary_op) gr_mul, ctx2, tol, state, 0);
gr_test_approx_binary_op(ctx, (gr_method_binary_op) gr_div, ctx2, tol, state, 0);

gr_test_approx_unary_op(ctx, (gr_method_unary_op) gr_neg, ctx2, tol, state, 0);
gr_test_approx_unary_op(ctx, (gr_method_unary_op) gr_sqr, ctx2, tol, state, 0);
gr_test_approx_unary_op(ctx, (gr_method_unary_op) gr_inv, ctx2, tol, state, 0);
gr_test_approx_unary_op(ctx, (gr_method_unary_op) gr_abs, ctx2, tol, state, 0);
}

GR_IGNORE(gr_one(tol, ctx2));
Expand Down

0 comments on commit 187b9bf

Please sign in to comment.