Pyrite

Pyrite Logo

Pyrite is a Crystal library for scientific computing, heavily inspired by the SciLua library. It provides a range of functionalities including complex numbers, special mathematical functions, pseudorandom number generators (PRNGs), root-finding algorithms, and statistical distributions. Pyrite is designed for applications requiring high performance and accurate computations in scientific and engineering domains.

Installation

Add this to your application's shard.yml:

dependencies:
  pyrite:
    github: qequ/pyrite

Then run:

$ shards install

Usage

Complex Numbers


require "pyrite/complex"

# Creating complex numbers
c1 = ComplexNumbers::Complex.new(2.0, 3.0)
c2 = ComplexNumbers::Complex.new(1.0, 3.0)

# Operations
sum = c1 + c2
difference = c1 - c2
product = c1 * c2
quotient = c1 / c2
magnitude = c1.abs

puts "Sum: #{sum}"
puts "Difference: #{difference}"
puts "Product: #{product}"
puts "Quotient: #{quotient}"
puts "Magnitude of c1: #{magnitude}"

Mathematical Functions

require "pyrite/math"

# Special functions and constants
puts "Pi: #{Pyrite::Math::PI}"
puts "Absolute value: #{Pyrite::Math.abs(-3.5)}"
puts "Phi function: #{Pyrite::Math.phi(1.0)}"

Pseudorandom Number Generators

require "pyrite/prng"

# Kiss99 PRNG
rng = Pyrite::PRNG::Kiss99.new
puts "Random number: #{rng.next_bits}"

Root-finding Algorithms

require "pyrite/root"

# Newton method example
f = ->(x : Float64) { x**2 - 2 }
f_prime = ->(x : Float64) { 2*x }
root, _ = Pyrite::Root.newton(f, f_prime, 0.0, 2.0) { false }
puts "Root of x^2 - 2: #{root}"

Statistical Distributions

Normal Distribution


require "pyrite/stat"

# Normal Distribution with mean 0 and standard deviation 1
normal_dist = Pyrite::Stat::Normal.new(0.0, 1.0)
puts "PDF of normal distribution at 0: #{normal_dist.pdf(0.0)}"
puts "Random sample from normal distribution: #{normal_dist.sample}"

Exponential Distribution

# Exponential Distribution with lambda = 1.5
exp_dist = Pyrite::Stat::Exponential.new(1.5)
puts "Mean of exponential distribution: #{exp_dist.mean}"
puts "Random sample from exponential distribution: #{exp_dist.sample}"

Uniform Distribution

# Uniform Distribution between 0 and 1
uniform_dist = Pyrite::Stat::Uniform.new(0.0, 1.0)
puts "PDF of uniform distribution at 0.5: #{uniform_dist.pdf(0.5)}"
puts "Random sample from uniform distribution: #{uniform_dist.sample}"
  

Gamma Distribution


# Gamma Distribution with alpha = 2.0 and beta = 3.0
gamma_dist = Pyrite::Stat::Gamma.new(2.0, 3.0)
puts "Mean of gamma distribution: #{gamma_dist.mean}"
puts "Random sample from gamma distribution: #{gamma_dist.sample}"
  

Beta Distribution

# Beta Distribution with alpha = 2.0 and beta = 5.0
beta_dist = Pyrite::Stat::Beta.new(2.0, 5.0)
puts "PDF of beta distribution at 0.5: #{beta_dist.pdf(0.5)}"
puts "Random sample from beta distribution: #{beta_dist.sample}"

Log-Normal Distribution


# Log-Normal Distribution with mu = 0 and sigma = 1
log_normal_dist = Pyrite::Stat::LogNormal.new(0.0, 1.0)
puts "PDF of log-normal distribution at 1: #{log_normal_dist.pdf(1.0)}"
puts "Random sample from log-normal distribution: #{log_normal_dist.sample}"

Student-t Distribution

# Student-t Distribution with nu = 10
student_dist = Pyrite::Stat::Student.new(10)
puts "PDF of student-t distribution at 0: #{student_dist.pdf(0.0)}"
puts "Random sample from student-t distribution: #{student_dist.sample}"

Development

After checking out the repo, run shards install to install dependencies. Then, run crystal spec to run the tests.

Roadmap

Pyrite is continuously evolving, and there are several features and enhancements planned for future releases. The following is a list of potential additions to the library:

These enhancements aim to make Pyrite a more comprehensive toolkit for scientific computing in Crystal, catering to a wide range of applications and user needs.

Contributing

  1. Fork it (https://github.com/qequ/pyrite/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

This project is based on and inspired by SciLua. The goal of Pyrite is to bring similar capabilities to the Crystal language, leveraging its performance and syntax features.