From 3b481848d19b530b01092c9513eb95f475290a77 Mon Sep 17 00:00:00 2001 From: Raoul Bourquin Date: Fri, 28 Oct 2022 21:17:36 +0200 Subject: [PATCH 1/3] Put golden ratio for ca into its own file --- ca.h | 2 ++ ca/phi.c | 21 +++++++++++++++++++++ ca/set_fexpr.c | 4 +--- doc/source/ca.rst | 4 ++++ 4 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 ca/phi.c diff --git a/ca.h b/ca.h index 0a12216..60949bf 100644 --- a/ca.h +++ b/ca.h @@ -317,6 +317,8 @@ void ca_pi(ca_t res, ca_ctx_t ctx); void ca_pi_i(ca_t res, ca_ctx_t ctx); void ca_euler(ca_t res, ca_ctx_t ctx); +void ca_phi(ca_t res, ca_ctx_t ctx); + void ca_unknown(ca_t x, ca_ctx_t ctx); void ca_undefined(ca_t x, ca_ctx_t ctx); diff --git a/ca/phi.c b/ca/phi.c new file mode 100644 index 0000000..1b1b28e --- /dev/null +++ b/ca/phi.c @@ -0,0 +1,21 @@ +/* + Copyright (C) 2020 Fredrik Johansson + Copyright (C) 2022 Raoul Bourquin + + This file is part of Calcium. + + Calcium 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 . +*/ + +#include "ca.h" + +void +ca_phi(ca_t res, ca_ctx_t ctx) +{ + ca_sqrt_ui(res, 5, ctx); + ca_add_ui(res, res, 1, ctx); + ca_div_ui(res, res, 2, ctx); +} diff --git a/ca/set_fexpr.c b/ca/set_fexpr.c index b103a59..a676e63 100644 --- a/ca/set_fexpr.c +++ b/ca/set_fexpr.c @@ -75,9 +75,7 @@ _ca_set_fexpr(ca_t res, fexpr_vec_t inputs, ca_vec_t outputs, const fexpr_t expr ca_euler(res, ctx); return 1; case FEXPR_GoldenRatio: - ca_sqrt_ui(res, 5, ctx); - ca_add_ui(res, res, 1, ctx); - ca_div_ui(res, res, 2, ctx); + ca_phi(res, ctx); return 1; case FEXPR_Infinity: ca_pos_inf(res, ctx); diff --git a/doc/source/ca.rst b/doc/source/ca.rst index 2cb01dc..739401d 100644 --- a/doc/source/ca.rst +++ b/doc/source/ca.rst @@ -349,6 +349,10 @@ Special values Sets *res* to Euler's constant `\gamma`. This creates an element of the (transcendental?) number field `\mathbb{Q}(\gamma)`. +.. function:: void ca_phi(ca_t res, ca_ctx_t ctx) + + Sets *res* to the golden ratio `\varphi`. + .. function:: void ca_unknown(ca_t res, ca_ctx_t ctx) Sets *res* to the meta-value *Unknown*. From c8c29932220e3e199547c2517ec233fe0caa8528 Mon Sep 17 00:00:00 2001 From: Raoul Bourquin Date: Fri, 28 Oct 2022 21:19:26 +0200 Subject: [PATCH 2/3] Simple example program computing the golden ratio --- examples/golden_ratio.c | 64 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 examples/golden_ratio.c diff --git a/examples/golden_ratio.c b/examples/golden_ratio.c new file mode 100644 index 0000000..ff728dd --- /dev/null +++ b/examples/golden_ratio.c @@ -0,0 +1,64 @@ +/* This file is public domain. Author: Raoul Bourquin. */ + +#include "ca.h" + + +void main_fexpr() +{ + fexpr_t Phi; + fexpr_init(Phi); + + flint_printf("Evaluating Phi as fexpr:\n"); + + fexpr_set_symbol_str(Phi, "GoldenRatio"); + + fexpr_print(Phi); + printf("\n\n"); + + fexpr_clear(Phi); +} + + +void main_ca() +{ + ca_ctx_t ctx; + ca_t Phi; + ca_ctx_init(ctx); + ca_init(Phi, ctx); + + flint_printf("Evaluating Phi as ca:\n"); + + ca_phi(Phi, ctx); + + ca_print(Phi, ctx); + printf("\n\n"); + + ca_clear(Phi, ctx); +} + + +void main_qqbar() +{ + qqbar_t Phi; + qqbar_init(Phi); + + flint_printf("Evaluating Phi as qqbar:\n"); + + qqbar_phi(Phi); + + qqbar_printn(Phi, 50); + printf("\n"); + + qqbar_clear(Phi); +} + + +int main(int argc, char *argv[]) +{ + main_fexpr(); + main_ca(); + main_qqbar(); + + flint_cleanup(); + return EXIT_SUCCESS; +} From aed4970d8a3171ad4b4acb1a731dad4ef0c162bd Mon Sep 17 00:00:00 2001 From: Raoul Bourquin Date: Fri, 28 Oct 2022 21:34:42 +0200 Subject: [PATCH 3/3] Single source of definition for phi Note: As calcium resolves quadratic fields, we still get phi explicitly as Q(sqrt(5)) instead of Q(a) with a^2-a-1 = 0. --- ca/phi.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/ca/phi.c b/ca/phi.c index 1b1b28e..38e566d 100644 --- a/ca/phi.c +++ b/ca/phi.c @@ -15,7 +15,11 @@ void ca_phi(ca_t res, ca_ctx_t ctx) { - ca_sqrt_ui(res, 5, ctx); - ca_add_ui(res, res, 1, ctx); - ca_div_ui(res, res, 2, ctx); + qqbar_t phi; + qqbar_init(phi); + qqbar_phi(phi); + + ca_set_qqbar(res, phi, ctx); + + qqbar_clear(phi); }