rat_trig package

Submodules

rat_trig.skeleton module

This is a skeleton file that can serve as a starting point for a Python console script. To run this script uncomment the following lines in the [options.entry_points] section in setup.cfg:

console_scripts =
     fibonacci = rat_trig.skeleton:run

Then run pip install . (or pip install -e . for editable mode) which will install the command fibonacci inside your current environment.

Besides console scripts, the header (i.e. until _logger…) of this file can also be used as template for Python modules.

Note

This file can be renamed depending on your needs or safely removed if not needed.

References

rat_trig.skeleton.fib(number: int) int[source]

Fibonacci example function

Parameters:

number (int) – integer

Returns:

n-th Fibonacci number

Return type:

int

F(1)=1  F(2)=1  F(3)=2  F(4)=3  F(5)=5  F(6)=8  ...   *     *     **    ***   ***** ******** ...
rat_trig.skeleton.main(args: List[str]) None[source]

Wrapper allowing fib() to be called with string arguments in a CLI fashion

Instead of returning the value from fib(), it prints the result to the stdout in a nicely formatted message.

Parameters:

args (List[str]) – command line parameters as list of strings (for example ["--verbose", "42"]).

rat_trig.skeleton.parse_args(args: List[str]) Namespace[source]

Parse command line parameters

Parameters:

args (List[str]) – command line parameters as list of strings (for example ["--help"]).

Returns:

command line parameters namespace

Return type:

argparse.Namespace

rat_trig.skeleton.run() None[source]

Calls main() passing the CLI arguments extracted from sys.argv

This function can be used as entry point to create console scripts with setuptools.

rat_trig.skeleton.setup_logging(loglevel: int) None[source]

Setup basic logging

Parameters:

loglevel (int) – minimum loglevel for emitting messages

rat_trig.trigonom module

Rational Trigonometry is a new approach to classical trigonometry, developed by Norman Wildberger, that aims to simplify and clarify the subject by using only rational numbers and operations, rather than irrational numbers and limits.

In traditional trigonometry, concepts such as the sine, cosine, and tangent of an angle are typically defined using circles and the unit circle in particular. These definitions involve irrational numbers and limits, which can make the subject more difficult to understand and work with.

In rational trigonometry, Wildberger replaces these circular definitions with ones based on lines and line segments, which allows for a more straightforward and intuitive approach. The fundamental concepts in rational trigonometry are the “quadaverage” and the “dilated directed angle,” which are defined in terms of lines and line segments, rather than circles.

Rational trigonometry has been gaining popularity in recent years, as it provides a useful alternative to traditional trigonometry for certain applications, such as computer graphics, robotics, and physics. It can also be a helpful tool for students who struggle with the irrational numbers and limits used in traditional trigonometry.

In summary, Rational Trigonometry is a new approach to classical trigonometry that uses rational numbers and operations, rather than irrational numbers and limits, making it a more straightforward and intuitive subject to understand and work with.

        A         |         |      q1 |  \ q3         |         |         B-----C           q2    where q1, q2, q3 are quadrances (squared distances)
class rat_trig.trigonom.NumType

Type variable for numeric types: int or Fraction.

alias of TypeVar(‘NumType’, int, ~fractions.Fraction)

rat_trig.trigonom.Numeric

int or Fraction.

Type:

Type alias for numeric types

alias of int | Fraction

rat_trig.trigonom.archimedes(q_1: int | Fraction, q_2: int | Fraction, q_3: int | Fraction) int | Fraction[source]

Archimedes’ formula for the quadrea of a triangle.

Given three side quadrances \(q_1, q_2, q_3\), the quadrea (signed area squared) is:

\[\mathcal{A} = 4 q_1 q_2 - (q_1 + q_2 - q_3)^2\]

This is also used to test whether four points \(Q_1, Q_2, Q_3, Q_4\) lie on a circle.

Parameters:
  • q_1 – First quadrance

  • q_2 – Second quadrance

  • q_3 – Third quadrance

Returns:

The quadrea of the triangle

Example

>>> from fractions import Fraction
>>> q_1 = Fraction(1, 2)
>>> q_2 = Fraction(1, 4)
>>> q_3 = Fraction(1, 6)
>>> archimedes(q_1, q_2, q_3)
Fraction(23, 144)
    A     |\\     | \\  q1 |  \\\ q3     |   \\     |    \\     B-----C       q2
rat_trig.trigonom.cross(v_1: Sequence[int | Fraction], v_2: Sequence[int | Fraction]) int | Fraction[source]

Scalar cross product of two 2D vectors.

For vectors \(\mathbf{v}_1 = (x_1, y_1)\) and \(\mathbf{v}_2 = (x_2, y_2)\):

\[\mathbf{v}_1 \times \mathbf{v}_2 = x_1 y_2 - y_1 x_2\]

This equals the signed area of the parallelogram spanned by the vectors.

Parameters:
  • v_1 – A sequence of two numbers

  • v_2 – A sequence of two numbers

Returns:

The scalar cross product

Example

>>> v_1 = [1, 2]
>>> v_2 = [3, 4]
>>> cross(v_1, v_2)
-2
     v2      ^      |      |    /      |   /      |  /      | / v1      |/____>     O
rat_trig.trigonom.dot(v_1: Sequence[int | Fraction], v_2: Sequence[int | Fraction]) int | Fraction[source]

Dot product of two 2D vectors.

For vectors \(\mathbf{v}_1 = (x_1, y_1)\) and \(\mathbf{v}_2 = (x_2, y_2)\):

\[\mathbf{v}_1 \cdot \mathbf{v}_2 = x_1 x_2 + y_1 y_2\]
Parameters:
  • v_1 – A sequence of two numbers

  • v_2 – A sequence of two numbers

Returns:

The dot product

Example

>>> v_1 = [1, 2]
>>> v_2 = [3, 4]
>>> dot(v_1, v_2)
11
     v2      ^      |\\      | \\      |  \\      |   \\      |    \\ v1      |     \\      |      \\      |_______\\     O         projection
rat_trig.trigonom.quad(vector: Sequence[int | Fraction]) int | Fraction[source]

Quadrance (squared length) of a 2D vector.

In rational trigonometry, quadrance replaces the classical notion of distance. For a vector \(\mathbf{v} = (x, y)\):

\[Q(\mathbf{v}) = x^2 + y^2\]

This is the squared Euclidean norm, always a rational number when \(x, y\) are rational.

Parameters:

vector – A sequence of two numbers

Returns:

The quadrance of the vector

Example

>>> vector = [3, 4]
>>> quad(vector)
25
    vector[1] ^              |              |\\              | \\              |  \\\  quad(vector) = vector[0]^2 + vector[1]^2              |   \\              |    \\              |     \\              |      \\              |_______\\\            O         vector[0]
rat_trig.trigonom.spread(v_1: Sequence[int | Fraction], v_2: Sequence[int | Fraction]) int | Fraction[source]

Spread between two 2D vectors.

In rational trigonometry, spread replaces the classical angle. It is the square of the sine of the angle between \(\mathbf{v}_1\) and \(\mathbf{v}_2\):

\[s(\mathbf{v}_1, \mathbf{v}_2) = \frac{(\mathbf{v}_1 \times \mathbf{v}_2)^2} {Q(\mathbf{v}_1) \; Q(\mathbf{v}_2)}\]

The result is always a rational number between 0 and 1.

Parameters:
  • v_1 – A sequence of two numbers

  • v_2 – A sequence of two numbers

Returns:

The spread between the two vectors

Example

>>> from fractions import Fraction
>>> v_1 = [Fraction(1), Fraction(2)]
>>> v_2 = [Fraction(3), Fraction(4)]
>>> spread(v_1, v_2)
Fraction(4, 125)
rat_trig.trigonom.spread_law(q_1: int | Fraction, q_2: int | Fraction, q_3: int | Fraction) int | Fraction[source]

Law of spreads for a triangle.

In any triangle with quadrances \(q_1, q_2, q_3\), the spread \(S_3\) opposite side \(q_3\) is:

\[S_3 = \frac{\mathcal{A}}{4 q_1 q_2} = \frac{4 q_1 q_2 - (q_1 + q_2 - q_3)^2}{4 q_1 q_2}\]

where \(\mathcal{A}\) is the quadrea from Archimedes’ formula.

Parameters:
  • q_1 – First quadrance

  • q_2 – Second quadrance

  • q_3 – Third quadrance (opposite the angle whose spread is found)

Returns:

The spread \(S_3\) opposite \(q_3\)

Example

>>> from fractions import Fraction
>>> q_1 = 5
>>> q_2 = 25
>>> q_3 = 20
>>> spread_law(q_1, q_2, q_3)
Fraction(4, 5)
rat_trig.trigonom.triple_quad_formula(q_1: int | Fraction, q_2: int | Fraction, s_3: int | Fraction) int | Fraction[source]

Triple quad formula relating two quadrances and a spread.

This formula relates two quadrances \(q_1, q_2\) and a spread \(s_3\) through the identity:

\[T(q_1, q_2, s_3) = (q_1 + q_2)^2 - 4 q_1 q_2 (1 - s_3) = (q_1 - q_2)^2 + 4 q_1 q_2 s_3\]

When \(s_3 = 1\) (right spread), this reduces to \((q_1 + q_2)^2\); when \(s_3 = 0\) (collinear), it reduces to \((q_1 - q_2)^2\).

Parameters:
  • q_1 – First quadrance

  • q_2 – Second quadrance

  • s_3 – Spread value

Returns:

Result of the triple quad formula

Example

>>> from fractions import Fraction
>>> q_1 = 5
>>> q_2 = 25
>>> s_3 = Fraction(4, 125)
>>> triple_quad_formula(q_1, q_2, s_3)
Fraction(416, 1)

Module contents

rat_trig - Rational Trigonometry

A Python implementation of Norman Wildberger’s Rational Trigonometry, which replaces traditional trigonometry concepts with quadrance (squared distance) and spread (squared sine of angle) for exact rational calculations.

Main functions: - archimedes: Calculate the quadrea of a triangle - cross: Calculate the cross product of two vectors - dot: Calculate the dot product of two vectors - quad: Calculate the quadrance of a vector - spread: Calculate the spread between two vectors - spread_law: Apply the law of spreads to a triangle - triple_quad_formula: Apply the triple quad formula

rat_trig.archimedes(q_1: int | Fraction, q_2: int | Fraction, q_3: int | Fraction) int | Fraction[source]

Archimedes’ formula for the quadrea of a triangle.

Given three side quadrances \(q_1, q_2, q_3\), the quadrea (signed area squared) is:

\[\mathcal{A} = 4 q_1 q_2 - (q_1 + q_2 - q_3)^2\]

This is also used to test whether four points \(Q_1, Q_2, Q_3, Q_4\) lie on a circle.

Parameters:
  • q_1 – First quadrance

  • q_2 – Second quadrance

  • q_3 – Third quadrance

Returns:

The quadrea of the triangle

Example

>>> from fractions import Fraction
>>> q_1 = Fraction(1, 2)
>>> q_2 = Fraction(1, 4)
>>> q_3 = Fraction(1, 6)
>>> archimedes(q_1, q_2, q_3)
Fraction(23, 144)
    A     |\\     | \\  q1 |  \\\ q3     |   \\     |    \\     B-----C       q2
rat_trig.cross(v_1: Sequence[int | Fraction], v_2: Sequence[int | Fraction]) int | Fraction[source]

Scalar cross product of two 2D vectors.

For vectors \(\mathbf{v}_1 = (x_1, y_1)\) and \(\mathbf{v}_2 = (x_2, y_2)\):

\[\mathbf{v}_1 \times \mathbf{v}_2 = x_1 y_2 - y_1 x_2\]

This equals the signed area of the parallelogram spanned by the vectors.

Parameters:
  • v_1 – A sequence of two numbers

  • v_2 – A sequence of two numbers

Returns:

The scalar cross product

Example

>>> v_1 = [1, 2]
>>> v_2 = [3, 4]
>>> cross(v_1, v_2)
-2
     v2      ^      |      |    /      |   /      |  /      | / v1      |/____>     O
rat_trig.dot(v_1: Sequence[int | Fraction], v_2: Sequence[int | Fraction]) int | Fraction[source]

Dot product of two 2D vectors.

For vectors \(\mathbf{v}_1 = (x_1, y_1)\) and \(\mathbf{v}_2 = (x_2, y_2)\):

\[\mathbf{v}_1 \cdot \mathbf{v}_2 = x_1 x_2 + y_1 y_2\]
Parameters:
  • v_1 – A sequence of two numbers

  • v_2 – A sequence of two numbers

Returns:

The dot product

Example

>>> v_1 = [1, 2]
>>> v_2 = [3, 4]
>>> dot(v_1, v_2)
11
     v2      ^      |\\      | \\      |  \\      |   \\      |    \\ v1      |     \\      |      \\      |_______\\     O         projection
rat_trig.quad(vector: Sequence[int | Fraction]) int | Fraction[source]

Quadrance (squared length) of a 2D vector.

In rational trigonometry, quadrance replaces the classical notion of distance. For a vector \(\mathbf{v} = (x, y)\):

\[Q(\mathbf{v}) = x^2 + y^2\]

This is the squared Euclidean norm, always a rational number when \(x, y\) are rational.

Parameters:

vector – A sequence of two numbers

Returns:

The quadrance of the vector

Example

>>> vector = [3, 4]
>>> quad(vector)
25
    vector[1] ^              |              |\\              | \\              |  \\\  quad(vector) = vector[0]^2 + vector[1]^2              |   \\              |    \\              |     \\              |      \\              |_______\\\            O         vector[0]
rat_trig.spread(v_1: Sequence[int | Fraction], v_2: Sequence[int | Fraction]) int | Fraction[source]

Spread between two 2D vectors.

In rational trigonometry, spread replaces the classical angle. It is the square of the sine of the angle between \(\mathbf{v}_1\) and \(\mathbf{v}_2\):

\[s(\mathbf{v}_1, \mathbf{v}_2) = \frac{(\mathbf{v}_1 \times \mathbf{v}_2)^2} {Q(\mathbf{v}_1) \; Q(\mathbf{v}_2)}\]

The result is always a rational number between 0 and 1.

Parameters:
  • v_1 – A sequence of two numbers

  • v_2 – A sequence of two numbers

Returns:

The spread between the two vectors

Example

>>> from fractions import Fraction
>>> v_1 = [Fraction(1), Fraction(2)]
>>> v_2 = [Fraction(3), Fraction(4)]
>>> spread(v_1, v_2)
Fraction(4, 125)
rat_trig.spread_law(q_1: int | Fraction, q_2: int | Fraction, q_3: int | Fraction) int | Fraction[source]

Law of spreads for a triangle.

In any triangle with quadrances \(q_1, q_2, q_3\), the spread \(S_3\) opposite side \(q_3\) is:

\[S_3 = \frac{\mathcal{A}}{4 q_1 q_2} = \frac{4 q_1 q_2 - (q_1 + q_2 - q_3)^2}{4 q_1 q_2}\]

where \(\mathcal{A}\) is the quadrea from Archimedes’ formula.

Parameters:
  • q_1 – First quadrance

  • q_2 – Second quadrance

  • q_3 – Third quadrance (opposite the angle whose spread is found)

Returns:

The spread \(S_3\) opposite \(q_3\)

Example

>>> from fractions import Fraction
>>> q_1 = 5
>>> q_2 = 25
>>> q_3 = 20
>>> spread_law(q_1, q_2, q_3)
Fraction(4, 5)
rat_trig.triple_quad_formula(q_1: int | Fraction, q_2: int | Fraction, s_3: int | Fraction) int | Fraction[source]

Triple quad formula relating two quadrances and a spread.

This formula relates two quadrances \(q_1, q_2\) and a spread \(s_3\) through the identity:

\[T(q_1, q_2, s_3) = (q_1 + q_2)^2 - 4 q_1 q_2 (1 - s_3) = (q_1 - q_2)^2 + 4 q_1 q_2 s_3\]

When \(s_3 = 1\) (right spread), this reduces to \((q_1 + q_2)^2\); when \(s_3 = 0\) (collinear), it reduces to \((q_1 - q_2)^2\).

Parameters:
  • q_1 – First quadrance

  • q_2 – Second quadrance

  • s_3 – Spread value

Returns:

Result of the triple quad formula

Example

>>> from fractions import Fraction
>>> q_1 = 5
>>> q_2 = 25
>>> s_3 = Fraction(4, 125)
>>> triple_quad_formula(q_1, q_2, s_3)
Fraction(416, 1)