Calling math.js functions

18 views
Skip to first unread message

Michael Johnson

unread,
Aug 29, 2017, 11:40:03 AM8/29/17
to TopBraid Suite Users
All,
I am trying to call the sqrt() function from the math.js library.
I have successfully created my own .js files and called functions with those, however calling functions from math.js seems to be a bit different

The math.js file is in my workspace.  

I have created different subclasses to spin:Functions called:
ex:math.sqrt
ex:sqrt
ex:_sqrtNumber


For example, in my ontology is the definition for sqrt:
ex:sqrt
  rdf:type spin:Function ;
  spin:constraint [
      rdf:type spl:Argument ;
      spl:predicate sp:arg1 ;
      spl:valueType rdfs:Literal ;
      rdfs:comment "The number to compute the square root of." ;
    ] ;
  spin:returnType xsd:float ;
  rdfs:subClassOf spin:Functions ;
.


I am trying to do a bind in my sparql - none of which work....
BIND (ex:math.sqrt(10) as ?root ).
BIND (ex:_sqrtNumber (9) as ?sqrt) .
BIND (ex:sqrt(9) as ?sqrt2) .

The actual function in the math.js file  looks something like this:

function factory (type, config, load, typed) {
  /**
   * Calculate the square root of a value.
   *
   * For matrices, the function is evaluated element wise.
   *
   * Syntax:
   *
   *    math.sqrt(x)
   *
   * Examples:
   *
   *    math.sqrt(25);                // returns 5
   *    math.square(5);               // returns 25
   *    math.sqrt(-4);                // returns Complex 2i
   *
   * See also:
   *
   *    square, multiply, cube, cbrt
   *
   * @param {number | BigNumber | Complex | Array | Matrix | Unit} x
   *            Value for which to calculate the square root.
   * @return {number | BigNumber | Complex | Array | Matrix | Unit}
   *            Returns the square root of `x`
   */
  var sqrt = typed('sqrt', {
    'number': _sqrtNumber,

    'Complex': function (x) {
        return x.sqrt();
    },

With a private function below: 

  /**
   * Calculate sqrt for a number
   * @param {number} x
   * @returns {number | Complex} Returns the square root of x
   * @private
   */
  function _sqrtNumber(x) {
    if (x >= 0 || config.predictable) {
      return Math.sqrt(x);
    }
    else {
      return new type.Complex(x, 0).sqrt();
    }
  }

How can I successfully call the sqrt() function?
I hope to be able to do the same with additional functions.

Thanks,
Michael


Holger Knublauch

unread,
Aug 29, 2017, 7:31:30 PM8/29/17
to topbrai...@googlegroups.com
Hi Michael,

in order to debug this best, could you send me all relevant files in the same set up as you have them? (Off-list). I would otherwise need to speculate and we'll have multiple back-and-forth emails.

In any case, the value spinx:math.js doesn't look like a correct file name. And you may want to try setting spinx:javaScriptCode instead of relying on the same-local-name policy. Defining the functions through some factory is going to complicate the situation, so better call them directly.

Reference: http://spinrdf.org/spinx.html

FWIW, from 5.4 onwards I'll recommend switching to SHACL-JS for user-defined functions.

Holger
--
You received this message because you are subscribed to the Google Groups "TopBraid Suite Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to topbraid-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Michael Johnson

unread,
Aug 30, 2017, 9:19:02 AM8/30/17
to TopBraid Suite Users
Hey Holger,
I did finally get it to work, but not in the way I thought I would (or how I understood the documentation).  
My steps:
1. Imported math.js into workspace
2. Created myFunctions.js.  In this file I make a direct call to Math.sqrt(x) 

function mysquare(x) 
{ return x * x; }
function myadd(x, y)
{ return x + y; }
function mysqrt(x)
  { return Math.sqrt(x) }

3. Created ex:mysqrt spin function (subclass of spin:Functions)
4. Within the ex:mysqrt spin function, I used the spinx:javascriptFile to call the myFunctions.js   
ex:mysqrt
  rdf:type spin:Function ;
  spin:returnType xsd:float ;
  <http://spinrdf.org/spinx#javaScriptFile> "spinx:myFunctions.js" ;
  rdfs:subClassOf spin:Functions ;
.
5. This allowed me to do the sparql bind:
BIND (ex:mysqrt(?x, 10) as ?mysqrt) .

Just wondering...do you have a time frame on TBC 5.4?   The shacl-js looks like where I want to go.  

Thanks!
Michael

Michael Johnson

unread,
Aug 30, 2017, 9:23:48 AM8/30/17
to TopBraid Suite Users
CORRECTION TO STEP 5:  
BIND (ex:mysqrt(?x) as ?mysqrt).  
Reply all
Reply to author
Forward
0 new messages