Using variables in CL calculations

While I was able to find examples of using simpleFunction as a way to perform calculations, I keep getting a syntax error when I want to display the values that I’m calculating.

Basically, I have a variable that is a rounded form of a video’s current time:
vidTimeRounded=numericValue(“\operatorname{round}\left(${vidTime},0\right)”)

I want to use vidTimeRounded in various mathematical expressions. For instance, I would like to be able to have 2^{vidTimeRounded} in order to create an exponential expression.

squareNum=simpleFunction(2^${vidTimeRounded})

However, when I go to display this in a Notes element,

content: “Square Time = ${squareNum}”

I get an error that reads, Unexpected type in string: functions<{“NumVars”:1}

It must just be a syntax thing.

Can someone help me? Thanks!

simpleFunction expects the function given as a latex, so you might get away with

simpleFunction(`2^${vidTimeRounded}`)

However, simpleFunction yields a function rather than a variable, so you need to “evaluate” it at a particular value. Therefore your best bet would be to have

squareNum=simpleFunction(`2^x`).evaluateAt(vidTimeRounded)

This creates the function 2^x and evaluates it at x=vidTimeRounded - thus making squareNum a value rather than a function, and should embed into your string as expected.

It may be that exponents in latex should be enclosed in curly braces:

squareNum= simpleFunction(`2^{ ${vidTimeRounded} }`)

Wowza. That was the solution!

When I was wandering about the documentation website, I didn’t feel like I was getting the same amount of detail out of it as I have with documentation from other scripting or coding docs.

Case in point: how did you learn about the return value types?

Also, you threw an x into simplefunction, but you didn’t declare it. So simplefunction just recognizes x or does it recognize any alpha character as a variable needing evaluation?

Thanks for your help and your patience!

That was my initial thought, too. Syntax is always my first go to in any coding language when errors are thrown. Turns out, though, I was misusing simplefunction. That line, itself had no errors thrown. It was when I tried to use the variable I’d assigned simplefunction’s return value to that I got stuck.

Thanks for jumping on this!

I think mainly through the documentation, looking at others’ work, asking and seeing a lot of the responses on here. The documentation does make some references to type (eg. in the sidebar of Computation Layer Documentation) but I think my early days of CL were a lot of trial-and-error, and unpicking other people’s activities to get an understanding.

I didn’t mention initially but another option if you wanted to generate a value would be numericValue - for instance, squareNum=numericValue(`2^{${vidTimeRounded}}`) would do the trick and is potentially simpler than simpleFunction. I tend to use the latter but that’s more out of habit now than preference.

simpleFunction recognises x without declaration, but any other or more variables must be included - eg. simpleFunction(`ut+0.5at^2`,`u`,`a`,`t`). This is then evaluated using eg. .evaluateAt(5,9.8,3)

Extremely helpful! Thank you, friend!

Sorry. Missed that you were looking for the numeric output.

  1. simpleFunction defaults to in terms of x. More or different variables need to be specified.
  2. You don’t need to have a variable present to evaluate (i.e. f(x)=constant). You could’ve used your prior expression without x and evaluated at any value:
squareNum= simpleFunction(`2^{ ${vidTimeRounded} }`).evaluateAt(0)

although I think @pirsquared 's solution looks cleaner.

  1. Without the evaluteAt( ), simpleFunction is a function object, not a value, and as such doesn’t have a latex or numeric output to display.

That’s an interesting feature of simpleFunction().evaluateAt() and a useful piece of information.

I’m already flying through this stuff thanks to your and pirsquared’s assistance.

Thanks again!