• Stars
    star
    106
  • Rank 316,242 (Top 7 %)
  • Language
    Julia
  • License
    MIT License
  • Created over 6 years ago
  • Updated about 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A symbolic math library written in Julia modelled off scmutils

Build Status Build status codecov

Symbolics.jl

This is a package I'm throwing together after getting inspired by the talk Physics in Clojure which was about porting scmutils to clojure. scmutils is a Scheme package with a very interesting and powerful computer algebra system meant as a companion to the book Structure and Interpretation of Classical Mechanics.

My intention with Symbolics.jl is to attempt to recreate the functionality of scmutils in julia using julian syntax. The package is slowly morphing into some sort of hybrid between scmutils and Mathematica.

This package works on Julia 1.0. To add it, simply

pkg> add https://github.com/MasonProtter/Symbolics.jl

Note: This package is very much a work in progress! Don't rely on it for anything important.

Examples of use:

  1. Basic algebra
julia > using Symbolics

julia> @sym x y z t;

julia> x^2 + x^2
2 * x ^ 2
  1. You can replace symbols in expressions
julia> ex = 2x + x^2
2x + x^2

julia> ex(x => y)
2y + y^2
  1. functional composition
julia> f(x) = x^3;

julia> g(x) = x^2;

julia> f + g
(::#70) (generic function with 1 method)

julia> ans(x)
x ^ 3 + x ^ 2

julia> f * g
(::#72) (generic function with 1 method)

julia> ans(x)
x ^ 5
  1. (Automatic) symbolic differentiation, now with higher derivatives and no pertubration confusion!
julia> D(f+g)(x)
3 * x ^ 2 + 2x

julia> (D^2)(f+g)(x)
3 * (2x) + 2

julia> (D^3)(f+g)(x)
6

The derivative operator, D is of type Dtype <: Operator <: Function. The reason for this is because operations on functions should sometimes behave differently than operations on differential operators. Currently the only difference is in exponentiation, such that :^(f::Function, n) = x -> f(x)^n whereas :^(o::Operator,n::Integer) = x -> o(o( ... o(x))) where the operator o has been applied to x n times.

  1. Symbolic expressions are callable and can be differentiated
julia> D(x(t)^2 + 2x(t), t)
2 * (x)(t) * (D(x))(t) + 2 * (D(x))(t)

New: Generate the Euler Lagrange Equations from a Lagrangian

We can now define a Lagrangian, say that of a simple harmonic oscillator as

using Symbolics

@sym x m ω t

function L(local_tuple::UpTuple)
    t, q, qdot = local_tuple.data
   (0.5m)*qdot^2 - (0.5m*ω^2)*q^2
end

where the local_tuple is an object describing a time, posisition and velocity (ie. all the relevant phase space data). According to SICM, this data should be provided by a function Γ(w) where w defines a trajectory through space. Γ is defined as

Γ(w) = t -> UpTuple([t, w(t), D(w)(t)])

Hence, as shown in SICM, the Euler-Lagrange condition for stationary action may be written as the functional

Lagrange_Equations(L) = w -> D(∂(3)(L)∘Γ(w)) - ∂(2)(L)∘Γ(w)

where ∂(3) means partial derivative with respect to velocity and ∂(2) means partial derivative with respect to position (ie. the third and second elements of the local tuple respectively). Putting this all together, we may execute

julia> Lagrange_Equations(L)(x)(t)
(D(D(x)))(t) * m + (x)(t) * m * ω ^ 2

which when set equal to zero is the equation of motion for a simple harmonic oscillator, generated in pure Julia code code symbolically!

More Repositories

1

Bumper.jl

Bring Your Own Stack
Julia
133
star
2

ReplMaker.jl

Simple API for building repl modes in Julia
Julia
126
star
3

Gaius.jl

Divide and Conquer Linear Algebra
Julia
114
star
4

SumTypes.jl

An implementation of Sum types in Julia
Julia
91
star
5

ShapeCheck.jl

Julia
35
star
6

MatrixProductStates.jl

DMRG using Matrix Product States in Julia
Julia
31
star
7

LegibleLambdas.jl

Provides a simple macro to make readable anonymous functions in Julia
Julia
28
star
8

NaturallyUnitful.jl

Facilities for using natural units with Unitful.jl
Julia
28
star
9

ToggleableAsserts.jl

Assertions that can be turned on or off with a switch, with no runtime penalty when they're off.
Julia
24
star
10

SymbolicTracing.jl

(experimental) tracing and abstract interpretation with SymbolicUtils.jl
Julia
22
star
11

GarbageMath.jl

Julia
18
star
12

StaticModules.jl

Static module-like namespaces for julia
Julia
18
star
13

Spec.jl

Automatic validation contexts
Julia
18
star
14

Currier.jl

Simple currying in julia
Julia
15
star
15

PatternDispatch.jl

Extensible multiple-dispatch style pattern matching in julia
Julia
13
star
16

MutableNamedTuples.jl

Basically what it says on the tin
Julia
13
star
17

SetupIterator.jl

Julia
11
star
18

Conjugates.jl

Goofy physics notation
Julia
11
star
19

SimpleUnderscores.jl

Because there weren't enough of these packages already
Julia
11
star
20

SpecializeVarargs.jl

Force specialization for varadic arguments in julia
Julia
9
star
21

GeometricMatrixAlgebras.jl

Geoemtric Algebra using julia matrices
Julia
6
star
22

InfixUnions.jl

Julia
5
star
23

BootlegCassette.jl

Julia
4
star
24

RewriteRepl.jl

Repl mode for Rewrite.jl
Julia
3
star
25

GeometricAlgebras.jl

Julia
3
star
26

.emacs.d

Emacs Lisp
3
star
27

BoseHubbard.jl

Julia
2
star
28

Curly.jl

Julia
2
star
29

MathExpressions.jl

Julia
1
star
30

GeometricAlgebra.jl

Julia
1
star
31

ContinuumBandStructure.jl

Julia
1
star