Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
run: ./siskin Rebol-MathPresso.nest --msvc -o mathpresso-windows-x64.rebx mathpresso-windows-x64

- name: Install Rebol for extension test
uses: oldes/install-rebol@v3.18.0
uses: oldes/install-rebol@v3.20.0

- name: Minimal Rebol/MathPresso extension test
run: ./rebol3 ci-test.r3
Expand All @@ -58,7 +58,7 @@ jobs:
run: ./siskin Rebol-MathPresso.nest -o mathpresso-linux-x64.rebx mathpresso-linux-x64

- name: Install Rebol for extension test
uses: oldes/install-rebol@v3.18.0
uses: oldes/install-rebol@v3.20.0

- name: Minimal Rebol/MathPresso extension test
run: ./rebol3 ci-test.r3
Expand Down Expand Up @@ -87,7 +87,7 @@ jobs:
run: ./siskin Rebol-MathPresso.nest -o mathpresso-macos-x64.rebx mathpresso-macos-x64

- name: Install Rebol for extension test
uses: oldes/install-rebol@v3.18.0
uses: oldes/install-rebol@v3.20.0

- name: Minimal Rebol/MathPresso extension test
run: ./rebol3 ci-test.r3
Expand Down
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2025 Oldes

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
149 changes: 88 additions & 61 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,61 +1,88 @@
[![rebol-mathpresso](https://github.com/user-attachments/assets/0541d4c8-f5dd-487f-94e4-53c29c44863c)](https://github.com/Siskin-framework/Rebol-MathPresso)

[![Rebol-MathPresso CI](https://github.com/Siskin-framework/Rebol-MathPresso/actions/workflows/main.yml/badge.svg)](https://github.com/Siskin-framework/Rebol-MathPresso/actions/workflows/main.yml)
[![Gitter](https://badges.gitter.im/rebol3/community.svg)](https://gitter.im/rebol3/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
[![Zulip](https://img.shields.io/badge/zulip-join_chat-brightgreen.svg)](https://rebol.zulipchat.com/)

# Rebol/MathPresso

[Rebol3](https://github.com/Oldes/Rebol3) extension for Mathematical Expression Parser And JIT Compiler.
Using Petr Kobalicek's [MathPresso code](https://github.com/kobalicek/mathpresso).

## Usage

This is an example of the initial syntax.

```rebol
mp: import 'mathpresso

;; initialize a mathpresso context with given number of input/output variable names
ctx: mp/context [x y step amplitude result]

;; compile an expression using the context
expr: mp/compile :ctx "y=sin(x); x=x+step; result=round(y*amplitude)/100"

;; To evaluate the expression, we need to provide a vector containing double values of count
;; eaqual or greater than number of variables used to create the evaluation context (5 in this case)
data: #(double! [0 0 0 10000 0]) ; used in the expression like x, y, step, amplitude and result values

;; initialize the step onput value using Rebol only
data/3: pi / 30

;; Evaluate expression (preferably multiple times)
loop 31 [ probe mp/eval :expr :data ]

;; Values in the data vector are updated...
probe data

;; one context may be shared with multiple expressions
expr2: mp/compile :ctx "y=sin(x)+cos(x/2); x=x+step; result=round(y*amplitude)/100"
loop 31 [ probe mp/eval :expr2 :data ]
```

Feel free to [let me know](https://gitter.im/rebol3/community) if something could be improved.

## Extension commands:


#### `context` `:spec`
Initialize MPContext handle with given variable names
* `spec` `[block!]` Block with variable names used by expressions

#### `compile` `:context` `:expression`
Compile math expression using the given context
* `context` `[handle!]` MPContext
* `expression` `[string!]` Math expression

#### `eval` `:expression` `:variables`
Evaluate precompiled math expressions using given variables
* `expression` `[handle!]` MPExpression
* `variables` `[vector!]` Variables in a double format

[![rebol-mathpresso](https://github.com/user-attachments/assets/0541d4c8-f5dd-487f-94e4-53c29c44863c)](https://github.com/Siskin-framework/Rebol-MathPresso)

[![Rebol-MathPresso CI](https://github.com/Siskin-framework/Rebol-MathPresso/actions/workflows/main.yml/badge.svg)](https://github.com/Siskin-framework/Rebol-MathPresso/actions/workflows/main.yml)
[![Gitter](https://badges.gitter.im/rebol3/community.svg)](https://gitter.im/rebol3/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
[![Zulip](https://img.shields.io/badge/zulip-join_chat-brightgreen.svg)](https://rebol.zulipchat.com/)

# Rebol/MathPresso

[Rebol3](https://github.com/Oldes/Rebol3) (3.19.1 and higher) extension for Mathematical Expression Parser And JIT Compiler.
Using Petr Kobalicek's [MathPresso code](https://github.com/kobalicek/mathpresso).

## Usage

This is an example of the initial syntax.

```rebol
mp: import 'mathpresso

;; Initialize a Mathpresso context using a struct containing some double values.
variables!: make struct! [
unused [uint64!] ;; this value is ignored by mathpresso expression
x [double!]
y [double!]
step [double!]
amplitude [double!]
result [double!]
]
ctx: mp/context :variables!

;; Compile an expression using the context (or directly the struct)
expr: mp/compile :ctx "y=sin(x); x=x+step; result=round(y*amplitude*100)/100"

;; To evaluate the expression, provide a struct of the same type used during compilation.
data: make variables! [
unused: 1234 ;; this value should not be affected by the evaluation
step: pi / 30 ;; initialize the step onput value using Rebol only
amplitude: 100 ;; maximum value of the result
]

;; Evaluate expression (preferably multiple times)
loop 31 [ probe mp/eval :expr :data ]

;; Values in the data struct were updated...
probe data

;; one context may be shared with multiple expressions
expr2: mp/compile :ctx {
y=sin(x)+cos(x/2);
x=x+step;
result=round(y*amplitude*100)/100
}
loop 31 [ probe mp/eval :expr2 :data ]
```

Feel free to [let me know](https://gitter.im/rebol3/community) if something could be improved.

## Extension commands:


#### `context` `:spec`
Initialize MPContext handle with given variable names
* `spec` `[struct!]` Struct with double fields used as expression variables

#### `compile` `:context` `:expression`
Compile math expression using the given context
* `context` `[struct! handle!]` Struct with double fields used by expression as variables or existing MPContext handle
* `expression` `[string!]` Math expression
* `/with`
* `flags` `[integer!]` Optional compilation flags

#### `eval` `:expression` `:variables`
Evaluate precompiled math expressions using given variables
* `expression` `[handle!]` MPExpression
* `variables` `[vector! struct!]` Variables in a double format

#### `set-options` `:flags`
MathPresso options used when expression is being compiled
* `flags` `[integer! none!]` Combination of: 1 = Verbose, 2 = DebugAst, 4 = DebugMachineCode, 8 = DebugCompiler


## Other extension values:
```rebol
;- Options used with the `set-options` command.
kOptionVerbose: 1 ;; Show messages and warnings.
kOptionDebugAst: 2 ;; Debug AST (shows initial and final AST).
kOptionDebugMachineCode: 4 ;; Debug machine code generated.
kOptionDebugCompiler: 8 ;; Debug AsmJit's compiler.
```
Loading