Symbolic Constants

The Giac.Constants module provides symbolic mathematical constants (pi, e, i) as GiacExpr values that preserve their symbolic form in expressions.

Why Use Symbolic Constants?

When you use Julia's native constants (like Base.pi) with GIAC expressions, they are converted to floating-point approximations:

using Giac

x = giac_eval("x")

# Using Julia's pi results in float conversion
expr = 2 * Base.pi * x
# Output: GiacExpr: 6.283185307179586*x  (FLOAT!)

With Giac.Constants, the symbolic form is preserved:

using Giac
using Giac.Constants: pi

x = giac_eval("x")

# Using Giac.Constants.pi stays symbolic
expr = 2 * pi * x
# Output: GiacExpr: 2*pi*x  (SYMBOLIC!)

Available Constants

ConstantDescriptionMathematical Symbol
piCircle constant (ratio of circumference to diameter)π
eEuler's number (base of natural logarithm)
iImaginary unit (square root of -1)i

Usage

Importing Constants

Constants are NOT exported from the main Giac module to avoid accidentally shadowing Julia's Base.pi. You must explicitly import them:

using Giac

# Option 1: Qualified access (recommended for clarity)
Giac.Constants.pi

# Option 2: Selective import
using Giac.Constants: pi, e, i

# Option 3: Import all constants
using Giac.Constants

Basic Operations

using Giac
using Giac.Constants: pi, e, i

# Arithmetic with symbolic constants
2 * pi                    # GiacExpr: 2*pi
pi / 2                    # GiacExpr: pi/2
3 * pi                    # GiacExpr: 3*pi

# Using in expressions with variables
x = giac_eval("x")
expr = 2 * pi * x         # GiacExpr: 2*pi*x

Trigonometric Functions

Trigonometric functions evaluate exactly when applied to symbolic constants:

using Giac
using Giac.Commands: sin, cos
using Giac.Constants: pi

# Convert to GiacExpr for use with invoke_cmd
pi_expr = convert(GiacExpr, pi)

invoke_cmd(:sin, pi_expr)      # GiacExpr: 0
invoke_cmd(:cos, pi_expr)      # GiacExpr: -1
invoke_cmd(:sin, pi_expr / 2)  # GiacExpr: 1

Euler's Formula

The classic identity e^(i*π) = -1 works symbolically:

using Giac
using Giac.Commands: exp
using Giac.Constants: pi, e, i

# Euler's formula
result = invoke_cmd(:exp, i * pi)
# Output: GiacExpr: -1

Notes on GIAC Behavior

GIAC normalizes some expressions automatically:

  • e is often displayed as exp(1) in output
  • e^2 becomes exp(2) (equivalent but normalized form)
  • pi remains symbolic and displays as pi
  • i remains symbolic and displays as i

This normalization is correct mathematical behavior - the expressions are equivalent.

Integration with Symbolics.jl

When using the GiacSymbolicsExt extension, constants convert correctly:

using Giac
using Symbolics
using Giac.Constants: pi

# Convert GIAC's symbolic pi to Symbolics.jl
pi_expr = convert(GiacExpr, pi)
sym_pi = to_symbolics(pi_expr)  # Returns Symbolics.pi

# Use in Symbolics.jl expressions
@variables x
expr = 2 * sym_pi * x  # Symbolics expression with π

API Reference

Giac.ConstantsModule
Giac.Constants

A submodule providing symbolic mathematical constants as GiacExpr values.

These constants preserve their symbolic form in expressions, unlike Julia's native constants which evaluate to floating-point approximations when used with GiacExpr.

Available Constants

  • pi: The circle constant π (ratio of circumference to diameter)
  • e: Euler's number ℯ (base of natural logarithm)
  • i: The imaginary unit √(-1)

Usage

using Giac
using Giac.Constants: pi, e, i

# Symbolic pi in expressions
x = giac_eval("x")
expr = 2 * pi * x
# Output: GiacExpr: 2*pi*x  (stays symbolic!)

# Compare with Base.pi (evaluates to float)
expr2 = 2 * Base.pi * x
# Output: GiacExpr: 6.283185307179586*x  (float!)

# Euler's formula
euler = e^(i * pi)
# Output: GiacExpr: -1

Why Use This Module?

When you write 2 * Base.pi * giac_expr, Julia's Base.pi (an Irrational) is converted to a float before multiplication. This module provides symbolic constants that remain symbolic throughout computations.

Note

Constants are NOT exported from the main Giac module to avoid shadowing Julia's Base.pi. You must explicitly import them:

# Option 1: Qualified access
Giac.Constants.pi

# Option 2: Selective import
using Giac.Constants: pi, e, i

# Option 3: Import all
using Giac.Constants
source
Giac.Constants.piConstant
pi

The circle constant π as a symbolic GiacExpr.

Example

using Giac
using Giac.Constants: pi

2 * pi  # Returns GiacExpr: 2*pi (symbolic, not float!)
sin(pi) # Returns GiacExpr: 0 (exact)
source
Giac.Constants.eConstant
e

Euler's number ℯ (base of natural logarithm) as a symbolic GiacExpr.

Example

using Giac
using Giac.Constants: e

log(e)    # Returns GiacExpr: 1 (exact)
e^2       # Returns GiacExpr: e^2 (symbolic)
source
Giac.Constants.iConstant
i

The imaginary unit √(-1) as a symbolic GiacExpr.

Example

using Giac
using Giac.Constants: i, pi, e

i^2           # Returns GiacExpr: -1
e^(i * pi)    # Returns GiacExpr: -1 (Euler's formula)
source