question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is it possible to treat a square root as it is, e.g. √5, without converting it to a number?

Specifically, I’d like to do the following:

sqrt(5) //=> √5 object
sqrt(2) + sqrt(3) //=> √2 + √3
sqrt(2) + sqrt(2) //=> 2 * √2
sqrt(3 * 3) //=> 3

I have read some examples and, like in the custom_datatatype.js example, I thought I could add a custom data type for √. But I couldn’t figure out how to return the value as it is of add, sub, etc.

If anyone knows how to do it right, please let me know! Finally, thank you for such a wonderful library.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
josdejongcommented, Aug 10, 2020

That’s good to hear 👍

About simplifying expressions with matrices: that is an open feature request, see #1913. Help is welcome 😃

1reaction
josdejongcommented, Aug 2, 2020

@yasuhito you could try another lib like https://nerdamer.com/demo or my lib while it is not supported here.

Thanks for the link Viktor, I hadn’t heard about Nerdamer before, it looks great! I’ve linked to it from the downloads page.

@yasuhito The reason is that there is a built-in rule which evaluates functions containing constants, also for sqrt. This is the simplifyConstant rule: simplify.js#L300. So, what you need is create your own version of this function, which first checks the special sqrt rule, and otherwise fallback to the old behavior. Here a full example:

const { simplify, isFunctionNode, isInteger, ConstantNode } = require('mathjs')

const rules = simplify.rules.map(rule => {
  if (typeof rule === 'function' && rule.name === 'simplifyConstant') {
    // replace the built-in simplifyConstant with our own extended version
    const simplifyConstant = rule

    return function customSimplifyConstant(node) {
      if (isFunctionNode(node) && node.name === 'sqrt') {
        const result = node.compile().evaluate({})
        if (isInteger(result)) {
          // return the integer result (for example `sqrt(25)`
          return new ConstantNode(result)
        } else {
          // do not replace sqrt functions not resulting in an integer result, like `sqrt(5)`
          return node
        }
      }
      else {
        // fall back on the original behavior
        return simplifyConstant(node, {})
      }
    }
  }
  else {
    // leave all other built-in rules untouched
    return rule
  }
})

function simplifyAndPrint(expression) {
  console.log(`${expression}=${simplify(expression, rules)}`)
}

simplifyAndPrint('2 + 5')     // 7
simplifyAndPrint('sqrt(25)')  // 5
simplifyAndPrint('sqrt(5)')   // sqrt(5)

You’re fully flexible in which rules you do or do not want to apply with the function simplify 😄

Read more comments on GitHub >

github_iconTop Results From Across the Web

Square root - Wikipedia
The symbol "√" for the square root was first used in print in 1525, in Christoph Rudolff's Coss.
Read more >
Python | sympy.sqrt() method - GeeksforGeeks
Returns: Returns square root in terms of values and symbolically simplified mathematical expression corresponding to the input expression.
Read more >
Symbolic sqrt(2) and sin(pi) - Specific Domains - Julia Discourse
Basic question on Julia symbolic. How to keep exp(1) as is and not convert it to a floating point number?
Read more >
What is square root symbol? - Definition from WhatIs.com
The square root symbol is used to indicate the quantity or quantities which, when multiplied by itself or themselves, results in the quantity...
Read more >
Function Reference: @sym/sqrt - Octave Forge - SourceForge
Note: this file is autogenerated: if you want to edit it, you might want to make changes to 'generate_functions.py' instead. Package: symbolic.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found