Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Symbolic parser for Julia language term rewriting using REDUCE algebra

License

NotificationsYou must be signed in to change notification settings

chakravala/Reduce.jl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Reduce.jl

Reduce.jl

Symbolic parser generator for Julia language expressions using REDUCE algebra term rewriter

DOIDocs StableDocs DevJoin the chat at gitterBuild StatusBuild statusCoverage Statuscodecov.io

The premise behind Reduce.jl is based on the idea thatSymbol andExpr types can be translated into computer algebra rewrite commands and then automatically parsed back into Julia ASTs, essentially extending the Julia language into a fully programable symbolic AST rewrite environment.

REDUCE is a system for general algebraic computations of interest to mathematicians, scientists and engineers:

  • exact arithmetic using integers and fractions; arbitrary precision numerical approximation;
  • polynomial and rational function algebra; factorization and expansion of polynomials and rational functions;
  • differentiation and integration of multi-variable functions; exponential, logarithmic, trigonometric and hyperbolic;
  • output of results in a variety of formats; automatic and user controlled simplification of expressions;
  • substitutions and pattern matching of expressions; quantifier elimination and decision for interpreted first-order logic;
  • solution of ordinary differential equations; calculations with a wide variety of special (higher transcendental) functions;
  • calculations involving matrices with numerical and symbolic elements; general matrix and non-commutative algebra;
  • powerful intuitive user-level programming language; generating optimized numerical programs from symbolic input;
  • Dirac matrix calculations of interest to high energy physicists; solution of single and simultaneous equations.

Interface for applying symbolic manipulation onJulia expressions usingREDUCE's term rewrite system:

  • reduce expressions areRExpr objects that canparse into juliaExpr objects and vice versa;
  • interface link communicates and interprets via various reduce output modes usingrcall method;
  • high-level reduce-julia syntax parser-generator walks arbitrary expression to rewrite mathematical code;
  • import operators from REDUCE using code generation to apply to arbitrary computational expressions;
  • interactivereduce> REPL within the Julia terminal window activated by} key;
  • extended arithmetic operators+,-,*,^,/,// compute onSymbol andExpr types;
  • provides hundreds of internal and external methods each supporting many argument types.

Additional packages that depend on Reduce.jl are maintained atJuliaReducePkg.

The upstream REDUCE software created by Anthony C. Hearn is maintained by collaborators onSourceForge.

This package is a heavily modifed version of Nathan Smith'sMaxima.jl with many additional features.

Setup

TheReduce package provides the base functionality to work with Julia and Reduce expressions, provided that you haveredcsl in your path. On GNU/Linux/OSX/Windows,Pkg.build("Reduce") will automatically download a precompiled binary for you. If you are running a different Unix operating system, the build script will download the source and attempt to compileredcsl for you, success depends on the build tools installed. Automated testing forTravis CI andappveyor using Linux, OSX, and Windows are fully operationalusing Reduce.

julia> Pkg.add("Reduce"); Pkg.build("Reduce")julia>using ReduceReduce (Free CSL version, revision4521),11-March-2018...

For users who wish to experimentally apply additional precompilation, it is possible to enable extra precompilation scripts by setting the environment variableENV["REDPRE"] = "1" in julia (only effective whenReduce is being compiled).

View the documentationstable /latest for more features and examples.

ThisReduce package for the Julia language was created bygithub.com/chakravala for mathematics and computer algebra research with the upstream developed REDUCE software.Please consider donating to show your thanks and appreciation to this Julia project for interfacing the upstream REDUCE language atliberapay,GitHub Sponsors,Patreon, or contribute (documentation, tests, examples) in the repository.

Usage

The extended algebraic symbolic expression mode of Reduce.jl is activated withForceImport.jl by

@forceusing Reduce.Algebra

This locally extends native Julia functions toSymbol andExpr types in the current module without extending global methods. Alternatively, the methods it provides can be accesed by prefixingAlgebra. in front of the method.

Reduce expressions encapsulated intoRExpr objects can be manipulated within julia using the standard syntax. Create an expression object either using theRExpr("expression") string constructor orR"expression". Additionally, arbitrary julia expressions can also be parsed directly using theRExpr(expr) constructor. InternallyRExpr objects are represented as an array that can be accessed by calling*.str[n] on the object.

WhenReduce is used in Julia, standard arithmetic operations are now extended to also work onSymbol andExpr types.

julia>1-1/:n:((n-1)// n)julia> ans^-:n:(1// ((n-1)// n)^ n)julia>limit(ans,:n,Inf)ℯ=2.7182818284590...

Julia abstract syntax trees are automatically converted into sequences of reduce statements (usingRExpr constructor) that are in return parsed into juliaquote blocks usigparse.Thercall method is used to evaluate any type of expression.

julia> :(int(sin(im*x+pi)^2-1,x))|> rcall:((1- (ℯ^ (4x)+4*^ (2x)* x))// (8*^ (2x)))

However, there are often multiple equivalent ways of achieving the same result:

julia>int(sin(im*:x+π)^2-1,:x):((1- (ℯ^ (4x)+4*^ (2x)* x))// (8*^ (2x)))

The output ofrcall will be the same as its input type.

julia>"int(sin(y)^2, y)"|> rcall"( - cos(y)*sin(y) + y)/2"

Usercall(expr,switches...) to evaluateexpr using REDUCE modeswitches like:expand,:factor, and:latex.

julia> :((x+im+π)^2;int(1/(1+x^3),x))|> RExpr^(+(x,i,pi),2);int(/(1,+(1,^(x,3))),x);julia>rcall(ans,:horner)|> parsequote    ((π+2x)* π+2*+ x)* im+ x^2)-1    ((2*sqrt(3)*atan((2x-1)//sqrt(3))-log((x^2- x)+1))+2*log(x+1))//6end

Mathematical operators and REDUCE modes can be applied directly toExpr andRExpr objects.

julia>Expr(:function,:(fun(a,b)),:(return4x^4-44x^3+61x^2+270x-525))|> horner:(functionfun(a, b)return ((4* (x-11)* x+61)* x+270)* x-525end)

Additionally, REDUCE switch statements can be used as macros to control evaluation of expressions.

julia>@rounded@factor x^3-2x+1:((x+1.61803398875)* (x-1)* (x-0.61803398875))

Most core features have a corresponding Julia method, but language features that have not been implemented yet can also be directly evaluated withrcall using a synergy of julia syntax.

julia>Expr(:for,:(i=2:34),:(product(i)))|> rcall:(@big_str"295232799039604140847618609643520000000")

Thesquash function provides a way to reduce full program blocks into simplified functions, e.g.

julia>Expr(:function,:(example(a,b)),quote           z=3           target= z*:a*:b           z-=1           target+= z*(1-:a)*(1-:b)end)|> squash|> factor:(functionexample(a, b)        (5b-2)* a-2* (b-1)end)

wherez is a program variable and:a and:b are symbolic variables.

Loading packages

Packages which come shipped with REDUCE can be loaded with theload_package method. For example, theoptimize method is available with

julia>load_package(:scope)julia> Algebra.optimize(:(z= a^2*b^2+10*a^2*m^6+a^2*m^2+2*a*b*m^4+2*b^2*m^6+b^2*m^2))quote    g40= b* a    g44= m* m    g41= g44* b* b    g42= g44* a* a    g43= g44* g44    z= g41+ g42+ g40* (2g43+ g40)+ g43* (2g41+10g42)end

Other packages can be loaded, but not all of them come with pre-defined Julia dispatch methods.

Matrices

Some special support for symbolic matrices has also been added toReduce.Algebra methods,

julia> [:x1;:y2]^-12×2 Array{Any,2}: :(2/ (2x- y))   :(-1/ (2x- y)) :(-y/ (2x- y))  :(x/ (2x- y))

Thejacobian method has been added to theReduceLinAlg package, which is dedicated to the LINALG extra package included with Reduce binaries.

julia>using ReduceLinAlgjulia> eqns= [:x1-:x2,:x1+:x2-:x3+:x6t,:x1+:x3t-:x4,2*:x1tt+:x2tt+:x3tt+:x4t+:x6ttt,3*:x1tt+2*:x2tt+:x5+0.1*:x8,2*:x6+:x7,3*:x6+4*:x7,:x8-sin(:x8)]8-element Array{Expr,1}: :(x1- x2) :(x1- ((x3- x6t)- x2)) :((x3t- x4)+ x1) :(x4t+ x6ttt+ x3tt+ x2tt+2x1tt) :((10x5+ x8+20x2tt+30x1tt)//10) :(2x6+ x7) :(3x6+4x7) :(x8-sin(x8))julia> vars= [:x1,:x2,:x3,:x4,:x6,:x7,:x1t,:x2t,:x3t,:x6t,:x7t,:x6tt,:x7tt];julia>jacobian(eqns, vars)|> Reduce.mat8×13 Array{Any,2}:1-10000000000011-10000001000100-100001000000000000000000000000000000000021000000000003400000000000000000000

The package also provides a demonstration of how additionalReduce methods can be imported into Julia.

Output mode

Various output modes are supported. While in the REPL, the defaultnat output mode will be displayed forRExpr objects.

julia> :(sin(x*im)+cos(y*MathConstants.φ))|> RExpr     (sqrt(5)+1)*ycos(-----------------)+sinh(x)*i2

This same output can also be printed to the screen by callingprint(nat(r)) method.

It is possible to direclty convert a julia expression object to LaTeX code using thelatex method.

julia>print(@latexsin(x)+cos(y*MathConstants.φ))\begin{displaymath}\cos\left(\left(\left(\sqrt {5}+1\right) y\right)/2\right)+\sin\,x\end{displaymath}

Internally, this command essentially expands torcall(:(sin(x) + cos(y*MathConstants.φ)),:latex) |> print, which is equivalent.

latex-equation

InIJulia the display output ofRExpr objects will be rendered LaTeX with therlfi REDUCE package inlatex mode.

REPL interface

Similar to? help and; shell modes in Julia,Reduce provides areduce> REPL mode by pressingshift+] as the first character in the julia terminal prompt. The output is innat mode.

reduce>df(atan(golden_ratio*x),x);22sqrt(5)*x+sqrt(5)- x+1-------------------------------422*(x+3*x+1)

Troubleshooting

If thereduce> REPL is not appearing when} is pressed or the Reduce pipe is broken, the session can be restored by simply callingReduce.Reset(), without requiring a restart ofjulia or reloading the package. This kills the currently running Reduce session and then re-initializes it for new use.

Otherwise, questions can be asked on gitter/discourse or submit your issue or pull-request if you require additional features or noticed some unusual edge-case behavior.

AbstractTensors interoperability

By importing theAbstractTensors.jl module, theReduce is able to correctly bypass operations onTensorAlgebra elements to the correct methods within the scope of theReduce.Algebra module.This requires no additional overhead for theGrassmann.jl orReduce packages, because theAbstractTensors interoperability interface enables separate precompilation of both.

Background

TheReduce package currently provides a robust interface to directly use the CSL version of REDUCE within the Julia language and the REPL. This is achieved by interfacing the abstract syntax tree ofExpr objects with the parser generator forRExpr objects and then using anIOBuffer to communicate withredpsl.

REDUCE is a system for doing scalar, vector and matrix algebra by computer, which also supports arbitrary precision numerical approximation and interfaces to gnuplot to provide graphics. It can be used interactively for simple calculations but also provides a full programming language, with a syntax similar to other modern programming languages.REDUCE has a long and distinguished place in the history of computer algebra systems. Other systems that address some of the same issues but sometimes with rather different emphasis are Axiom, Macsyma (Maxima), Maple and Mathematica.REDUCE is implemented in Lisp (as are Axiom and Macsyma), but this is completely hidden from the casual user. REDUCE primarily runs on either Portable Standard Lisp (PSL) or Codemist Standard Lisp (CSL), both of which are included in the SourceForge distribution. PSL is long-established and compiles to machine code, whereas CSL is newer and compiles to byte code. Hence, PSL may be faster but CSL may be available on a wider range of platforms.

Releases ofReduce.jl enable the general application of various REDUCE functionality and packages to manipulate the Julia language to simplify and compute new program expressions at run-time. Intended for uses where a symbolic pre-computation is required for numerical algorithm code generation.

Julia is a high-level, high-performance dynamic programming language for numerical computing. It provides a sophisticated compiler, distributed parallel execution, numerical accuracy, and an extensive mathematical function library. Julia’s Base library, largely written in Julia itself, also integrates mature, best-of-breed open source C and Fortran libraries for linear algebra, random number generation, signal processing, and string processing.The strongest legacy of Lisp in the Julia language is its metaprogramming support. Like Lisp, Julia represents its own code as a data structure of the language itself. Since code is represented by objects that can be created and manipulated from within the language, it is possible for a program to transform and generate its own code. This allows sophisticated code generation without extra build steps, and also allows true Lisp-style macros operating at the level of abstract syntax trees.


[8]ページ先頭

©2009-2025 Movatter.jp