EzDevInfo.com

mathjs

An extensive math library for JavaScript and Node.js math.js | an extensive math library for JavaScript and Node.js math.js is an extensive math library for javascript and node.js. it features big numbers, complex numbers, matrices, units, and a flexible expression parser.

How to get value form Input as numbers to calculate gcd?

I use math.js to calculate gcd for a series of numbers wich are taken from input. Among the tested options I mention:

  • parseInt() and parseFloat()
  • IaNr=document.getElementById("nrs").value
  • math.gcd(nrs.value);
  • math.gcd($("#nrs").val())
  • IaNr.toString()

but it doesn't work. what am I missing, because it doesn't calculate the numbers from input. it works when I provide the numbers directly. ex: math.gcd(4, 8, 25,74)

<script src="http://cdnjs.cloudflare.com/ajax/libs/mathjs/1.2.0/math.min.js"></script>  

$(function(){

IaNr=$("#nrs").val()

DoGcd=math.gcd(IaNr)

$("#show").html(DoGcd)
})          

<input id="nrs"  value="14,92,34,78">

<div id="show"></div>

Source: (StackOverflow)

Searching for (any) strings in another string separated by non-alphanumeric characters

How can I extract words (which can be anything) out of a string which are separated by non-alphabetic characters (digits or symbols) and save the result in an array.

For example if I parsed the following I would like to have the name of the three fruits in an array.

var input str = '= ((1 * bananas ^ 5) - oranges / mangos)'  // to get [bananas, oranges, mangos]

The practical application of this is that, I would like to extract variable names from a mathematical formula, after which I can assign values to them (which I'd get from some object or array)


Source: (StackOverflow)

Advertisements

How can I avoid exponential notation in mathjs results

In a function that uses mathjs would like to have the results displayed as 1000000000000000000000000000 instead of 1e+27.

I tried the math.format() function, but didn't work.

> x = 1e+27
< 1e+27
> math.format(x, {notation: 'fixed'})
< "1e+27"
> math.format(x, {notation: 'auto', exponential: {lower: 1e-100, upper: 1e+100}})
< "1e+27"

Source: (StackOverflow)

Is Math.js capable of evaluating expressions in a topological order ?

math.eval(["c = b" , "a = b + c"] , {"a" : 1, "b" : 2})
[2, 4]

Switching the order of the expressions,

math.eval(["a = b + c" , "c = b"] , {"a" : 1, "b" : 2})
Error: Undefined symbol c

Setting initial value for c to NaN

math.eval(["a = b + c" , "c = b"] , {"a" : 1, "b" : 2, c: NaN})
[NaN, 2]

Is math.js capable of evaluating expressions in a topological order ?


Source: (StackOverflow)

equation javascript calculating

I have code:

 <!DOCTYPE html>
    <html>
    <body>
    <script>
    function compute(){
      var input=document.getElementById("calculator");
      var number=input.value;
      var result=calculate(number);
      document.getElementById("demo").innerHTML=result;
     }
    function calculate(number){
    var result=1;
    for(var i=3; i<= number; i++)
    {
    result*=(number*number+2);
    }
    return result;
    }

    </script>
    <input id="calculator" />
    <button onclick="compute()">Calculate</button>
    <p id="demo"></p>

    </body>
    </html> 

My result should be sum of the equations (n²+2) when i=3 to n. I know that result*=(number*number+2); is not good formula, it is example. If you know, please help me. Thanks guys


Source: (StackOverflow)

Precision decimals, 30 of them, in JavaScript (Node.js)

My Challenge

I am presently working my way through reddit's /r/dailyprogrammer challenges using Node.js and have caught a snag. Being that I'm finishing out day 3 with this single exercise, I've decided to look for help. I refuse to just move on without knowing how.

Challenge #6: Your challenge for today is to create a program that can calculate pi accurately to at least 30 decimal places.

My Snag

I've managed to obtain the precision arithmetic I was seeking via mathjs, but am left stumped on how to obtain 30 decimal places. Does anyone know a library, workaround or config that could help me reach my goal?

/*jslint node: true */
"use strict";

var mathjs = require('mathjs'),
  math = mathjs();

var i,
  x,
  pi;

console.log(Math.PI);

function getPi(i, x, pi) {
  if (i === undefined) {
    pi = math.eval('3 + (4/(2*3*4))');
    i = 2;
    x = 4;
    getPi(i, x, pi);
  } else {
      pi = math.eval('pi + (4/('+x+'*'+x+1+'*'+x+2+')) - (4/('+x+2+'*'+x+3+'*'+x+4+'))');
      x += 4;
      i += 1;
    if (x < 20000) {
      getPi(i, x, pi);
    } else {
      console.log(pi);
    }
  }
}

getPi();

I have made my way through many interations of this, and in this example am using the Nilakatha Series:

Nilakatha Series


Source: (StackOverflow)

es5 parameters shadowing troubles in safari

In my Grunt/yeoman/angular project, I have part of the code written as node modules and imported by Grunt during the build project with browserify. it happens that, only on safari(checked on 7 and 8), the minified version of the webapp doesn't work because of this error:

SyntaxError: Cannot declare a parameter named 'k' in strict mode

I discovered that the line of code is this one:

c.prototype.key=function k(k){var a=this._baseState;return f(null===a.key),a.key=k,this}

And the starting code is:

Node.prototype.key = function key(key) {
  var state = this._baseState;

    assert(state.key === null);
      state.key = key;

        return this;
};

I don't understand if it is part of the browserify libraries, but surely it's included when I add mathjs to the project. I tried reserving the word "key" with uglifyJS mangle options, but "key" is a reserved word, too.

How can I avoid this kind of problems? Am I using the wrong approach?

EDIT: I found the function in asn1 lib https://github.com/indutny/asn1.js/blob/master/lib/asn1/base/node.js#L225 Now I don't really know how to go on.

EDIT: The author of the library has fixed the issue istantly :) https://github.com/indutny/asn1.js/commit/c75d861e705df9559bf572e682552278b98a218d


Source: (StackOverflow)

How can I plot with respect to a variable in mathjs?

I'm using mathjs to parse expressions that are inputted by the user however I can't figure out how to actually let a user type in something like this:

0.3x + 2

All I can do right now is either hardcode x into the function to make it in respect to x, or just plot lines where y is the same number regardless of x.

In particular since I am graphing with respect to x because of the for-loop, I'd like to be able to use the variable x in my equations

Anyone know how to add variables to functions in mathjs?

Thanks.

http://jsfiddle.net/brockwhittaker/vkgLfau6/3/

EDIT: Of course, right as I finally ask I figure it out.

Here's a working fiddle: http://jsfiddle.net/brockwhittaker/vkgLfau6/4/


Source: (StackOverflow)

Converting a string of comma separated numbers into numbers

I am trying get a list of comma separated numbers to process in math.js library (https://github.com/josdejong/mathjs/blob/master/docs/functions.md) in Node.js.

While I got the list list of numbers like "2,4,5" and did a comma split to store in an array. The library refused to process my array using it's math.add() function. It needs individual integers to process. I could use the function with array[0],array[1] but I want to provide all the integers at once using a single variable.

exports.add = function(req, res) {
    var v = req.params.v;
    var array = v.split(",");
    var intarray = [];
    for(var i=0; i<array.length; i++) {
        intarray[i] = +array[i];
    }
    var result = math.add(intarray);
    res.end(result.toString());
}

Source: (StackOverflow)

How to inject mathjs into Angular.js?

How can I inject mathjs into Angular.js?

I can't get it to work - I used bower install mathjs --save to install it.

Update: The JS min file is properly injected into index.html

Do I need to inject it into the main module or into the controller? I tried both and got a white screen on page load with an error message about the non-availability of the module.


Source: (StackOverflow)

How can I reference and update elements of a matrix in javascript/node?

I have to update/retrieve values from a matrix based on the index, for example, a[2,3] refers to the element in the second row and third column, that can now be read or set.

Is there a way to do this with sylvester ?

I think that the subset function in mathjs does this.

Is there a more elegant way to do this in javascript/node ?


Source: (StackOverflow)

Import dependencies in ember-cli (e.g., import math.js)

I am puzzled about importing dependencies in ember-cli, especially about the standard AMD case, as mentioned in the official Ember Cli document. The document doesn't provide much of examples, and seems to me it assumes the readers have good knowledge about AMD, which is not the case for me. My immediate use case is import math.js. Unfortunately, the official document of math.js doesn't provide example about importing with Ember Cli. Then, I found this post with relatively clearer examples, in particular, the following one seems quite relevant.

app.import({
  development: 'vendor/lodash/dist/lodash.js',
  production:  'vendor/lodash/dist/lodash.min.js'
}, {
  'lodash': [
    'default'
  ]
}); 

Then, I did similar thing with math.js like below:

app.import({
  development: 'bower_components/mathjs/dist/math.js',
  production:  'bower_components/mathjs/dist/math.min.js'
}, {
  'mathjs': [
    'default'
  ]
});

However, it doesn't work. When I tried to use it with

import mathjs from 'mathjs'

I got an error. Eventually, I used the following solution:

// Brocfile.js
app.import('bower_components/mathjs/dist/math.min.js');

// some controller.js
var math = window.math

Although above solution works, I don't like it as it may be subject to name conflict. Besides, based on math.js's document, it seems to me it should support standard AMD type of import.

So, my questions are the following.
1. In the lodash example above, what does the 'default' mean? Is that a general reference to whatever exported in the corresponding module? How can I tell whether I can use it in general (for example, math.js)?
2. Is it true that if a module supports require.js, then it is a standard AMD module? If so, given code like below:

require.config({
  paths: {
    mathjs: 'path/to/mathjs',
  }
});
require(['mathjs'], function (math) {
  // use math.js
  math.sqrt(-4); // 2i
});

how can I map it to Ember Cli code?


Source: (StackOverflow)