{ "cells": [ { "cell_type": "markdown", "source": [ "Building a declination table\n", "============================" ], "metadata": {} }, { "cell_type": "code", "execution_count": null, "source": [ "from kanon.units import Sexagesimal\n", "from kanon.units.precision import set_precision, get_context, TruncatureMode\n", "from kanon.tables import HTable\n", "from kanon.tables.htable import join_multiple\n", "import math\n", "\n", "# For these calculations we will round after every operations.\n", "\n", "get_context().mutate(tmode=TruncatureMode.ROUND)" ], "outputs": [], "metadata": {} }, { "cell_type": "code", "execution_count": null, "source": [ "OBLIQUITY = \"23;51,20\"" ], "outputs": [], "metadata": { "tags": [ "parameters" ] } }, { "cell_type": "code", "execution_count": null, "source": [ "# We construct a table of sine from 0 to 90.\n", "x = list(Sexagesimal.range(91))\n", "y = [round(Sexagesimal.from_float(math.sin(math.radians(n)), 3)) for n in x]\n", "sin_table = HTable([x, y], names=(\"Arg\", \"Val\"), index=\"Arg\")\n", "\n", "sin_table" ], "outputs": [], "metadata": {} }, { "cell_type": "code", "execution_count": null, "source": [ "# We apply a multiplication on all the sine values to get obl_table = Sin(x) * obl\n", "\n", "obl = sin_table.get(Sexagesimal(OBLIQUITY))\n", "\n", "obl_table = sin_table.apply(\"Val\", lambda x: x * obl)\n", "obl_table\n" ], "outputs": [], "metadata": {} }, { "cell_type": "code", "execution_count": null, "source": [ "# Arcsin is sin_table taken from Val to Arg instead of Arg to Val.\n", "\n", "arcsin_table = sin_table.copy(set_index=\"Val\")\n", "\n", "# For example, if we want arcsin(0.3) :\n", "\n", "arcsin_table.get(0.3)\n" ], "outputs": [], "metadata": {} }, { "cell_type": "code", "execution_count": null, "source": [ "# Finally, we apply arcsin_table.get on every obl_table values to find decl_table = arcsin(obl_table)\n", "\n", "decl_table = obl_table.apply(\"Val\", arcsin_table.get)\n", "decl_table" ], "outputs": [], "metadata": {} }, { "cell_type": "code", "execution_count": null, "source": [ "# Let's make the same calculation with an interpolated sine table.\n", "\n", "# First, extract every 12th row until the 60th then every 6th.\n", "\n", "sin_table_grid = sin_table[\n", " [i for i in range(91) if i <= 60 and i % 12 == 0 or i > 60 and i % 6 == 0]\n", "]\n", "\n", "sin_table_grid" ], "outputs": [], "metadata": {} }, { "cell_type": "code", "execution_count": null, "source": [ "# We can populate it with its interpolation method (linear by default).\n", "\n", "sin_table_pop_linear = sin_table_grid.populate(list(Sexagesimal.range(91)), method=\"interpolate\")\n", "\n", "sin_table_pop_linear" ], "outputs": [], "metadata": {} }, { "cell_type": "code", "execution_count": null, "source": [ "# You can use other types of interpolation, for example a quadratic interpolation.\n", "\n", "from kanon.tables.interpolations import quadratic_interpolation\n", "\n", "sin_table_grid.interpolate = quadratic_interpolation\n", "\n", "# This interpolation method needs a higher precision to avoid weird results.\n", "with set_precision(pmode=5):\n", " sin_table_pop_quadratic = sin_table_grid.populate(list(Sexagesimal.range(91)), method=\"interpolate\")\n", "\n", "sin_table_pop_quadratic" ], "outputs": [], "metadata": {} }, { "cell_type": "code", "execution_count": null, "source": [ "# Another type of interpolation can come from filling a interpolation grid made from populate\n", "# With this you can use methods interpolating the whole table at once, like euclidean distribution interpolations\n", "\n", "sin_table_pop_euclidean = sin_table_grid.populate(list(Sexagesimal.range(91)))\n", "sin_table_pop_euclidean = sin_table_pop_euclidean.fill(\"distributed_convex\")\n", "\n", "sin_table_pop_euclidean" ], "outputs": [], "metadata": {} }, { "cell_type": "code", "execution_count": null, "source": [ "# Comparing the 2nd difference between the true sine table and the interpolated ones.\n", "\n", "true_2diff = sin_table.diff(n=2, new_name=\"True Sine 2-diff\")\n", "interpolated_linear_2diff = sin_table_pop_linear.diff(n=2, new_name=\"Linear Sine 2-diff\")\n", "interpolated_quadratic_2diff = sin_table_pop_quadratic.diff(n=2, new_name=\"Quadratic Sine 2-diff\")\n", "interpolated_euclidean_2diff = sin_table_pop_euclidean.diff(n=2, new_name=\"Distributed Sine 2-diff\")\n", "\n", "# Now we have 4 tables with the same arguments. We can compare them side to side by joining them.\n", "\n", "join_multiple(true_2diff, interpolated_linear_2diff, interpolated_quadratic_2diff, interpolated_euclidean_2diff)" ], "outputs": [], "metadata": {} }, { "cell_type": "code", "execution_count": null, "source": [ "# With this newly built table we are able to construct a new declination table.\n", "\n", "obl_linear = sin_table_pop_linear.get(Sexagesimal(OBLIQUITY))\n", "\n", "obl_table_linear = sin_table_pop_linear.apply(\"Val\", lambda x: x * obl)\n", "arcsin_table_linear = sin_table_pop_linear.copy(set_index=\"Val\")\n", "\n", "obl_table_linear.apply(\"Val\", arcsin_table_linear.get)" ], "outputs": [], "metadata": {} } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.0" } }, "nbformat": 4, "nbformat_minor": 4 }