1 | ;
|
2 |
|
3 | require('./../utils/polyfills');
|
4 |
|
5 | var isFactory = require('./../utils/object').isFactory;
|
6 |
|
7 | var typedFactory = require('./typed');
|
8 |
|
9 | var emitter = require('./../utils/emitter');
|
10 |
|
11 | var importFactory = require('./function/import');
|
12 |
|
13 | var configFactory = require('./function/config');
|
14 | /**
|
15 | * Math.js core. Creates a new, empty math.js instance
|
16 | * @param {Object} [options] Available options:
|
17 | * {number} epsilon
|
18 | * Minimum relative difference between two
|
19 | * compared values, used by all comparison functions.
|
20 | * {string} matrix
|
21 | * A string 'Matrix' (default) or 'Array'.
|
22 | * {string} number
|
23 | * A string 'number' (default), 'BigNumber', or 'Fraction'
|
24 | * {number} precision
|
25 | * The number of significant digits for BigNumbers.
|
26 | * Not applicable for Numbers.
|
27 | * {boolean} predictable
|
28 | * Predictable output type of functions. When true,
|
29 | * output type depends only on the input types. When
|
30 | * false (default), output type can vary depending
|
31 | * on input values. For example `math.sqrt(-4)`
|
32 | * returns `complex('2i')` when predictable is false, and
|
33 | * returns `NaN` when true.
|
34 | * {string} randomSeed
|
35 | * Random seed for seeded pseudo random number generator.
|
36 | * Set to null to randomly seed.
|
37 | * @returns {Object} Returns a bare-bone math.js instance containing
|
38 | * functions:
|
39 | * - `import` to add new functions
|
40 | * - `config` to change configuration
|
41 | * - `on`, `off`, `once`, `emit` for events
|
42 | */
|
43 |
|
44 |
|
45 | exports.create = function create(options) {
|
46 | // simple test for ES5 support
|
47 | if (typeof Object.create !== 'function') {
|
48 | throw new Error('ES5 not supported by this JavaScript engine. ' + 'Please load the es5-shim and es5-sham library for compatibility.');
|
49 | } // cached factories and instances
|
50 |
|
51 |
|
52 | var factories = [];
|
53 | var instances = []; // create a namespace for the mathjs instance, and attach emitter functions
|
54 |
|
55 | var math = emitter.mixin({});
|
56 | math.type = {};
|
57 | math.expression = {
|
58 | transform: {},
|
59 | mathWithTransform: {} // create a new typed instance
|
60 |
|
61 | };
|
62 | math.typed = typedFactory.create(math.type); // create configuration options. These are private
|
63 |
|
64 | var _config = {
|
65 | // minimum relative difference between two compared values,
|
66 | // used by all comparison functions
|
67 | epsilon: 1e-12,
|
68 | // type of default matrix output. Choose 'matrix' (default) or 'array'
|
69 | matrix: 'Matrix',
|
70 | // type of default number output. Choose 'number' (default) 'BigNumber', or 'Fraction
|
71 | number: 'number',
|
72 | // number of significant digits in BigNumbers
|
73 | precision: 64,
|
74 | // predictable output type of functions. When true, output type depends only
|
75 | // on the input types. When false (default), output type can vary depending
|
76 | // on input values. For example `math.sqrt(-4)` returns `complex('2i')` when
|
77 | // predictable is false, and returns `NaN` when true.
|
78 | predictable: false,
|
79 | // random seed for seeded pseudo random number generation
|
80 | // null = randomly seed
|
81 | randomSeed: null
|
82 | /**
|
83 | * Load a function or data type from a factory.
|
84 | * If the function or data type already exists, the existing instance is
|
85 | * returned.
|
86 | * @param {{type: string, name: string, factory: Function}} factory
|
87 | * @returns {*}
|
88 | */
|
89 |
|
90 | };
|
91 |
|
92 | function load(factory) {
|
93 | if (!isFactory(factory)) {
|
94 | throw new Error('Factory object with properties `type`, `name`, and `factory` expected');
|
95 | }
|
96 |
|
97 | var index = factories.indexOf(factory);
|
98 | var instance;
|
99 |
|
100 | if (index === -1) {
|
101 | // doesn't yet exist
|
102 | if (factory.math === true) {
|
103 | // pass with math namespace
|
104 | instance = factory.factory(math.type, _config, load, math.typed, math);
|
105 | } else {
|
106 | instance = factory.factory(math.type, _config, load, math.typed);
|
107 | } // append to the cache
|
108 |
|
109 |
|
110 | factories.push(factory);
|
111 | instances.push(instance);
|
112 | } else {
|
113 | // already existing function, return the cached instance
|
114 | instance = instances[index];
|
115 | }
|
116 |
|
117 | return instance;
|
118 | } // load the import and config functions
|
119 |
|
120 |
|
121 | math['import'] = load(importFactory);
|
122 | math['config'] = load(configFactory);
|
123 | math.expression.mathWithTransform['config'] = math['config']; // apply options
|
124 |
|
125 | if (options) {
|
126 | math.config(options);
|
127 | }
|
128 |
|
129 | return math;
|
130 | }; |
\ | No newline at end of file |