Skip to content

Variable as parameter of custom function #3156

Answered by josdejong
NullDev asked this question in Q&A
Discussion options

You must be logged in to vote

@NullDev you can use the rawArgs feature, then you can use sigmaSum(n=1, k=5, n^2 + k) and
f(n) = sigmaSum(i=1, k=n-1, floor(1 / gcd(i, k))); f(15) just like you want.

Docs: https://mathjs.org/docs/expressions/customization.html#custom-argument-parsing

Here a full example:

import { create, all } from 'mathjs'

const mathjs = create(all)

function sigmaSum (args, math, scope){
  const [nNode, kNode, exprNode] = args

  if (!nNode.isAssignmentNode || !nNode.object.isSymbolNode) {
    throw Error('First argument must define a variable, like "i=1"')
  }
  const nName = nNode.object.name

  if (!kNode.isAssignmentNode || !kNode.object.isSymbolNode) {
    throw Error('Second argument must defin…

Replies: 2 comments 14 replies

Comment options

dvd101x
Feb 13, 2024
Collaborator Sponsor

You must be logged in to vote
2 replies
@NullDev
Comment options

@dvd101x
Comment options

dvd101x Feb 14, 2024
Collaborator Sponsor

Comment options

You must be logged in to vote
12 replies
@NullDev
Comment options

@josdejong
Comment options

@josdejong
Comment options

@josdejong
Comment options

@NullDev
Comment options

Answer selected by NullDev
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants