Examples Gallery
This page provides a variety of snippets showing Zigma in action across different scientific and engineering domains.
1. Projectile Motion (Kinematics)
This example calculates the trajectory of a projectile and demonstrates how Zigma derives complex units like Joules and Watts from base SI units.
g = 9.81 m/s2
v0 = 50 m/s
angle = 45 // degrees (dimensionless)
mass = 2 kg
// Breaking velocity into components
vx = v0 * cos(angle)
vy = v0 * sin(angle)
// Calculate Kinetic Energy (Automatically becomes kg.m2/s2)
Ek = 0.5 * mass * v0^2
print "Initial Kinetic Energy: {Ek in J}"
// Calculate time of flight
t_total = (2 * vy) / g
print "Time of flight: {t_total}"
2. Ideal Gas Law (Thermodynamics)
Using custom aliases to make equations look exactly like they do in a textbook.
alias Pa as N/m2
alias R_const as 8.314 J/(mol.K)
n = 1 mol
T = 300 K
V = 0.0224 m3
// P = nRT / V
P = (n * R_const * T) / V
print "Pressure: {P in Pa}"
print "Pressure in bar: {P in bar}"
3. Stress and Strain (Structural Engineering)
This example uses a table to look up Material Properties (Young's Modulus) and
calculates the deformation of a steel beam under load.
// material_data.csv: Material, E [GPa], Density [kg/m3]
materials = table("materials.csv")
E_steel = in materials get E as Material = "Steel"
area = 10 cm * 10 cm
length = 5 m
force = 5000 N
// Stress = Force / Area
stress = force / area
// Strain = Stress / Young's Modulus
strain = stress / E_steel
// Delta Length = Strain * Original Length
dl = strain * length
print "Total elongation: {dl in mm}"
4. 3D Vector Rotation (Linear Algebra)
Zigma uses SIMD to accelerate matrix-vector multiplications.
// A 3D position vector
pos = [10, 0, 0] m
// A 3x3 Rotation Matrix (dimensionless)
R_z = [[ 0, -1, 0],
[ 1, 0, 0],
[ 0, 0, 1]]
// Rotate the vector using matrix multiplication operator (@)
new_pos = R_z @ pos
print "Original: {pos}"
print "Rotated: {new_pos}" // [0, 10, 0] m
5. Lab Measurement (Uncertainty Propagation)
Calculate the density of an object given measurements with sensor errors. Zigma propagates the uncertainty automatically.
mass = 500.5 +/- 0.1 g
radius = 2.5 +/- 0.05 cm
// Volume of a sphere: 4/3 * pi * r^3
volume = (4/3) * 3.14159 * radius^3
// Density = Mass / Volume
density = mass / volume
print "Object Density: {density in kg/m3}"
// Output will include the +/- error margin automatically
6. Financial Ledger (Custom Dimensions)
Zigma isn't just for physics; it can track any "dimension" you define.
dimension usd
alias eur as 1.08 * usd
price_eur = 50 eur
tax_rate = 0.20 // 20%
total_usd = (price_eur * (1 + tax_rate)) in usd
print "Total Price: {total_usd:.2} USD"
7. High-Precision Orbital Mechanics
Using i128 fixed-point math to calculate the gravitational force between
planets without floating-point drift.
as i128:
G = 6.674e-11 m3/(kg.s2)
m_earth = 5.972e24 kg
m_moon = 7.348e22 kg
r = 384400 km
// F = G * (m1 * m2) / r^2
force = G * (m_earth * m_moon) / r^2
print "Gravitational Force: {force in PN}" // Peta-Newtons