A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://lurchmath.github.io/openmath-js/site/api-reference below:

Reference - OpenMath.js Documentation

API Reference Getting started In the browser

Import the JavaScript, which you can download from our repository directly or import from a CDN with the following one-liner.

<script src='https://cdn.jsdelivr.net/npm/openmath-js@1/openmath.js'></script>
From the command line

Or install this package into your project the usual way:

npm install openmath-js

Then within any of your modules, import it as follows.

OM = import( "openmath-js" ).OM;

After that, any of the example code snippets in this documentation should function as-is.

Creating OpenMath objects

The prototype for OpenMath data structures (that is, expression trees) is named OMNode in the global namespace (the browser window) and is also named OM for convenience; they are the same object. It is defined as an ES6 class.

Rather than use its constructor, there are a number of factory functions that create OM instances, as follows.

Example use:

plus = OM.symbol( 'plus', 'arith1' ); arg1 = OM.variable( 'x' ); arg2 = OM.integer( 5 ); OM.application( plus, arg1, arg2 ).toXML(); // gives XML encoding for x+5

Because the above method can easily become annoyingly lengthy, we also provide a shorthand for writing OpenMath expressions as strings and having them parsed in a convenient way. Full details are covered in a separate page about simple encoding and decoding. Here is a brief summary.

Example usage:

OM.simpleDecode( 'arith1.plus(x,5)' ).toXML();

Each of the functions in this section have nicknames. For each factory function given above, it has a three-letter nickname to help you write shorter code that builds OpenMath tree structures. The nicknames are all in the OM namespace, and include int, flo, str, byt, sym, var, app, att, bin, and err. Thus, for instance, you can write the following code to build a valid OpenMath expression.

OM.app( OM.sym( 'plus', 'arith1' ), OM.var( 'x' ), OM.int( 5 ) ).toXML();

Finally, the simpleDecode function also has the nickname simple, so the most compact form is the following.

OM.simple( 'arith1.plus(x,5)' ).toXML();

The OM objects are just wrappers around JSON tree structures that provide methods for interacting with those tree structures. You can get access to the tree structur itself with myInstance.tree. It is not quite JSON, because it has circular references, as children nodes point to their parent nodes, but it is close to JSON.

The specification for how OpenMath expressions are stored as JSON trees is given in comments at the top of the source code, should you need it. The following methods are available for working with such structures, but these are rarely used by the client, and are mostly for internal purposes.

Writing/saving OpenMath objects Properties of OpenMath Objects

Note that each of these properties is actually produced by a getter function, and thus is not always as efficient as you might think. For instance, you may not wish to write loops like this:

for ( var i = 0 ; i < anOMinstance.children.length ; i++ )
    process( anOMinstance.children[i] ); // calls getter many times

Rather, you might do better with a loop like this:

for ( var i = 0, ch = anOMinstance.children ; i < ch.length ; i++ )
    process( ch[i] ); // doesn't call getter at all on this line

As mentioned at the end of the first section, it is possible to create two different OM instances that refer to the same internal tree structure. And as mentioned immediately above, calling instance.children[i] produces a new instance each time you call it. Thus instance.children[0] and instance.children[0] will refer to the same internal JSON structure, but will be different OM instances. We can thus check equality in two ways.

Examples:

A = OM.application( OM.variable( 'f', OM.integer( 3 ) ) ); // f(3) B = A.copy(); // deep copy C = A.children[0]; // builds a new OM instance for the f D = A.children[0]; // does the same thing again console.log( A.equals( B ) ); // true console.log( A.sameObjectAs( B ) ); // false console.log( A == B ); // false console.log( C.equals( D ) ); // true console.log( C.sameObjectAs( D ) ); // true console.log( C == D ); // false

You can also modify tree structures as follows.

Searching OpenMath Trees

We have devised a way for indexing and addressing children and descendants within parent/ancestor trees, and the following functions use that convention. You can read about the indexing/addressing convention in the source code documentation, the section entitled "Parent-Child Relationships."

Example:

A = OM.application( OM.variable( 'print' ), OM.string( 'Hello' ), OM.string( 'World' ) ); str1 = A.children[1]; // zero-based console.log( str1.value ); // "Hello" index = str1.findInParent(); console.log( index ); // "c1" child = A.findChild( index ); console.log( child.sameObjectAs( str1 ) ); // true

Examples:

deepTree = OM.simple( 'arith1.plus(f(g(x,y)),h(k(z)))' ); descendant = deepTree.index( [ 'c1', 'c1', 'c2' ] ); console.log( descendant.name ); // "y" console.log( descendant.address( deepTree ) ); // c1,c1,c2 console.log( descendant.address( deepTree.children[1] ) ); // c1,c2

You can filter children or descendants by predicates.

Free and Bound Variables

Many applications of OpenMath relate to logic and/or programming, in which variable binding and substitutin plays a critical role. The following functions make it easy to ask questions and perform the most common operations related to variable binding and substitution.

Miscellany

Sometimes it is useful to be able to take any JavaScript string and convert it into a string that could be used as a valid OpenMath identifier (such as a variable or symbol name). Because only a subset of Unicode is permitted, we provide an injection (although not a very compact one) from all strings into the set of strings accepted as valid OpenMath identifiers. The range of the function is strings of the form "id_[many decimal digits here]".

Example:

console.log( OM.encodeAsIdentifier( '#$&@' ) ); // id_0023002400260040 console.log( OM.decodeIdentifier( "id_0023002400260040" ) ); // #$&@

Some applications find it useful to be able to evaluate simple numerical OpenMath expressions.

Example:

console.log( OM.simple( 'transc1.cos(0)' ).evaluate() ); // 1 console.log( OM.simple( 'f(x)' ).evaluate() ); // error message console.log( OM.simple( 'e' ).evaluate() ); // 2.71828... w/rounding message

More Examples

In addition to the brief examples shown in this file, the test suite in the source code repository is (naturally) a large set of examples of how the module works, and they become useful approximately where "factory functions" are tested.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4