1. Construct transcendental expressions¶
Construct a transcendental application with mkTerm, just as you would
construct addition or a comparison. Its argument is a symbolic real term;
building the expression does not evaluate a machine floating-point function.
Prerequisite: sorts and symbolic terms.
Use the existing term interface¶
"""Construct transcendental expressions and exact real constants."""
from std.testing import assert_true
from cvc5_mojo import TermManager, Kind
def main() raises:
var tm = TermManager()
var real = tm.getRealSort()
var x = tm.mkConst(real, "x")
var exponential = tm.mkTerm(Kind.EXPONENTIAL, x)
var sine = tm.mkTerm(Kind.SINE, x)
var cosine = tm.mkTerm(Kind.COSINE, x)
var tangent = tm.mkTerm(Kind.TANGENT, x)
var inverse_sine = tm.mkTerm(Kind.ARCSINE, x)
var pi = tm.mkTerm(Kind.PI)
var one = tm.mkTerm(Kind.TO_REAL, tm.mkInteger(1))
var two = tm.mkTerm(Kind.TO_REAL, tm.mkInteger(2))
var half = tm.mkTerm(Kind.DIVISION, one, two)
assert_true(exponential.getSort() == real)
assert_true(sine.getSort() == real)
assert_true(cosine.getSort() == real)
assert_true(tangent.getSort() == real)
assert_true(inverse_sine.getSort() == real)
assert_true(pi.getKind() == Kind.PI)
assert_true(half.getSort() == real)
print("exponential:", exponential)
print("sine:", sine)
print("cosine:", cosine)
print("tangent:", tangent)
print("inverse sine:", inverse_sine)
print("pi:", pi)
print("exact half:", half)
Run from the repository root pixi run --locked mojo run -I src docs/examples/transcendental_terms.mojo
Output
exponential: (exp x)
sine: (sin x)
cosine: (cos x)
tangent: (tan x)
inverse sine: (arcsin x)
pi: real.pi
exact half: (/ (to_real 1) (to_real 2))
The program demonstrates exponential, sine, cosine, tangent, and inverse
sine expressions. It constructs symbolic π with mkTerm(Kind.PI);
the generated API also provides the mkPi() convenience method.
The pinned upstream test exercises this zero-argument mkTerm construction
too. Upstream term-construction tests
The sort assertions verify construction. They do not claim that every equation containing these operators will be decided. In particular, making an inverse-trigonometric term is different from obtaining a convenient numeric value for it.
Operator names¶
This table is generated from the pinned headers. It lists expression constructors, not a promise that arbitrary formulas using them are easy to solve.
| Kind | Meaning in the pinned release |
|---|---|
Kind.EXPONENTIAL |
Exponential function. |
Kind.SINE |
Sine function. |
Kind.COSINE |
Cosine function. |
Kind.TANGENT |
Tangent function. |
Kind.COSECANT |
Cosecant function. |
Kind.SECANT |
Secant function. |
Kind.COTANGENT |
Cotangent function. |
Kind.ARCSINE |
Arc sine function. |
Kind.ARCCOSINE |
Arc cosine function. |
Kind.ARCTANGENT |
Arc tangent function. |
Kind.ARCCOSECANT |
Arc cosecant function. |
Kind.ARCSECANT |
Arc secant function. |
Kind.ARCCOTANGENT |
Arc cotangent function. |
Kind.PI |
Pi constant. |
Use the enum reference for the complete inventory. There is no logarithm kind in this pinned inventory. The next chapter introduces a logarithm using an exponential relation instead.
Construct exact constants¶
The example converts integer terms to real terms and divides them to represent one half exactly. The printed expression retains those conversions; construction does not necessarily simplify the expression to its shortest form.
The helper in the bounds example packages this same construction for several rational constants. All its denominators are nonzero. If a denominator is symbolic, exclude zero when the intended specification is ordinary real division.
Symbolic π is also different from a decimal approximation. Replacing it
with 314159/100000 changes the formula. That difference matters at a zero
of sine, at the boundary of a phase interval, or near a tangent pole.
Use a rational approximation only when the approximation and its error are
explicitly part of the model.
Select the arithmetic extension¶
The solving examples set the logic to QF_NRAT before asserting formulas.
This follows cvc5's
pinned transcendental example.
Keep real variables for the arguments. Constructing a real term does not
turn a real problem into floating-point arithmetic.
For equalities, ensure both sides have the same sort. A direct equality
between a real expression and an integer term can raise a sort error; use
Kind.TO_REAL for the integer numeral, as the executable examples do.
Exercise: express Euler's number¶
How would you represent the mathematical constant e without using a
rounded decimal?
Worked answer. Construct a real one and apply Kind.EXPONENTIAL to it.
The term denotes exp(1). Its existence as a symbolic expression does not
imply that a model query will print an exact finite decimal; no such
decimal exists.
Next: exponentials and logarithms.