Console apps - axkr/symja_android_library GitHub Wiki

Symja can be used as computer algebra system in a REPL (Read-Eval-Print-Loop)

Usage

Read-Eval-Print-Loop

The diagram below shows the overall design of the Symja command line programs:

flowchart LR
    Input-->|string|Scanner
    subgraph Read
    Scanner-->|tokens|Parser
    end
    subgraph  Eval-Print
    Parser-->|AST-abstract syntax tree|Eval
    Eval-->Print
    end

REPL image

The first functional block, the READ phase, reads some text containing code and creates a syntax tree with an internal representation of the input program.

The Scanner separates the input text in tokens (the building "text blocks" of the program) and then the Parser takes this series of tokens and produces an Abstract Syntax Tree (AST), a hierarchical representation of the source code.

Once we have an AST we’ll be able to evaluate the expression to produce a result in the EVAL phase. This phase will be more complex than what we’ve seen until now, the EvalEngine#eval() method will recursively evaluate the AST and return the resulting evaluated symbolic expression.

The built-in symbols and functions defined by Symja are implemented in the BuiltInSymbol class with their name and corresponding function attributes. The built-in functions are implemented in the package org.matheclipse.core.builtin

These functions will take an IAST parameter containing the original list (function name and arguments), evaluate it and return an IExpr as result. If the IAST parameter couldn't be evaluated the function returns the special static object NIL (not-in-list) from the F.class.

In the PRINT phase the result from the EVAL phase will be transformed back to an output string with the OutputFormFactory class. This string will then be printed on screen for the user and a new math expression input can be typed into the screen's input line.

There are some Symja functions which can create output strings in a special format like:

We provide a way to render plots in the PRINT phase without a servlet engine or web server. To do that, we write an output HTML file to disk and use the default browser on the desktop to load it.

These Symja functions render plots in an output HTML file to disk: