A little proof-of-concept for organizing mathjs by module inclusion, avoiding factory functions.
Go to file
Glen Whitney bcbb24acd2 feat: Generic numerical types
Inspired by https://github.com/josdejong/mathjs/discussions/2212 and
  https://github.com/josdejong/mathjs/issues/2585.

  Provides a simple adapter function `adapted` which takes a class
  implementing an arithmetical datatype and returns a PocomathInstance
  with a new type for that class, invoking the methods of the class
  in a standard way for the Pocomath/mathjs operations.  (That instance
  can then be installed in another to add the new type to any instance
  you like, including the default one.)

  Uses this facility to bring fraction.js Fraction into Pocomath, and
  tests the resulting type.

  Currently the "standard" interface for an arithmetical type is heavily modeled
  after the design of fraction.js, but with experience with other 3rd-party types
  it could be streamlined to something pretty generic (and the Fraction
  adaptation could be patched to conform to the resulting "standard"). Or a
  proposal along the lines of https://github.com/josdejong/mathjs/discussions/2212
  could be adopted, and a shim could be added to fraction.js to conform to
  **that** standard.

  Resolves #30.
2022-08-07 09:19:27 -07:00
src feat: Generic numerical types 2022-08-07 09:19:27 -07:00
test feat: Generic numerical types 2022-08-07 09:19:27 -07:00
.gitignore chore: Initialize project with pnpm 2022-07-18 15:33:08 -07:00
LICENSE Initial commit 2022-07-18 22:10:03 +00:00
README.md doc(README): Add brief notes about new template facilities 2022-08-07 03:34:39 +00:00
package.json5 feat: Generic numerical types 2022-08-07 09:19:27 -07:00
pnpm-lock.yaml feat: Generic numerical types 2022-08-07 09:19:27 -07:00

README.md

pocomath

A little proof-of-concept for organizing mathjs by module inclusion, avoiding factory functions.

Note this project is package-managed by pnpm. I do not expect that a clone can easily be manipulated with npm.

Defines a class PocomathInstance to embody independent instances of a mathjs-style CAS. Basically, it keeps track of a collection of implementations (in the sense of typed-function) for each of the functions to be used in the CAS, rather than just the finalized typed-functions. It also tracks the dependencies of each implementation (which must form a directed acyclic network). When a method is requested from the instance, it assembles the proper typed-function (and caches it, of course). Whenever an implementation is added to that function name or any of its dependencies, the previously assembled typed-function is discarded, so that a new one will be constructed on its next use.

Multiple different instances can coexist and have different collections of operations. Moreover, only the source files for the operations actually desired are ever visited in the import tree, so minimizing a bundle for a specific subset of operations should be quite straightforward.

Hopefully the test cases, especially test/_pocomath.mjs and test/custom.js, will show off these aspects in action.

Note that 'subtract' is implemented as a 'generic' operation, that depends only on the 'add' and 'negate' operations (and so doesn't care what types it is operating on). Although it would not be the computationally fastest in a production instance, for the sake of demonstration 'divide' and 'sign' are also so defined.

Furthermore, note that 'Complex' is implemented in a way that doesn't care about the types of the real and imaginary components, so with the 'bigint' type defined here as well, we obtain Gaussian integers for free.

This core could be extended with many more operations, and more types could be defined, and additional sub-bundles like number/all.mjs or clever conditional loaders like complex/extendToComplex.mjs could be defined.

Also see the comments for the public member functions of core/PocomathInstance.mjs for further details on the structure and API of this scheme for organizing a CAS.

Hopefully this shows promise. It is an evolution of the concept first prototyped in picomath. However, picomath depended on typed-function allowing mutable function entities, which turned out not to be performant. Pocomath, on the other hand, uses typed-function v3 as it stands, although it does suggest that it would be helpful to extend typed-function with subtypes, and it could even be reasonable to move the dependency tracking into typed-function itself (given that typed-function already supports self-dependencies, it would not be difficult to extend that to inter-dependencies between different typed-functions).

Note that Pocomath allows one implementation to depend just on a specific signature of another function, for efficiency's sake (if for example 'bar(Matrix)' knows it will only call 'foo(Matrix)', it avoids another type-dispatch). That capability is used in sqrt, for example.

Pocomath also lazily reloads operations that depend on the config when that changes, and if an operation has a signature mentioning an undefined type, that signature is ignored until the type is installed, at which point the function lazily redefines itself to use the additional signature.

Pocomath now also allows template operations and template types, also built on top of typed-function (but candidates for integration therein). This is used to make many operations more specific, implement a type-homogeneous Tuple type, and make Complex numbers be type-homogeneous (which it seems like it always should be). One of the cutest consequences of this approach is that with careful definitions of the Complex<T> templates, one gets a working quaternion data type absolutely for free as Complex<Complex<number>> (and integral quaternions as Complex<Complex<bigint>>, etc.)