Numerical solver for systems of nonlinear equations?

16 views
Skip to first unread message

J Heitzig

unread,
Nov 9, 2023, 1:39:45 PM11/9/23
to webppl-dev
I'm trying to figure out how to solve some nontrivial nonlinear equations within WebPPL. I found Ceres.js, but its initialization runs asynchronously, which gives me a headache since I can't use `await` within WebPPL... Even if I enforce a long sleep via

  Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 100000);

the Ceres module initialization promise still hasn't resolved.

Is there any way to deal with promises returned by js functions within WebPPL? Or do you know of any other solver that runs synchronously?

null-a

unread,
Nov 9, 2023, 2:18:58 PM11/9/23
to webppl-dev
A while back I made a webppl package that provides a `callAsync` function that can be used to call async JS functions from within webppl.


Maybe you can use this directly, else perhaps it will gives you enough clues to get where you need to be.

Also, a word of (unsolicited) advice... The distinction and interoperability between webppl and js code is a very common source of confusion. Not that I think you are confused! Nevertheless, if you've not seen them already, it might be worth taking a peek at these notes:


Best,
Paul

J Heitzig

unread,
Nov 9, 2023, 4:26:53 PM11/9/23
to webppl-dev
Wow, that works like a charm!
I'm curious how you did that? I don't understand the `resumeTrampoline` thing...

null-a

unread,
Nov 9, 2023, 5:10:36 PM11/9/23
to webppl-dev
Great!

> I don't understand the `resumeTrampoline` thing...

To be expected! It's an internal thing that exists specifically to support this use case.


The gist of what's going on, is that webppl code is transformed / compiled into continuation passing style[1] JS, which is then run via a "trampoline" [2] to avoid blowing the stack. `resumeTrampoline` gives us a way to restart this execution once the async thing is done. (Or something like that anyway, it's been a while...)


Best,
Paul
Reply all
Reply to author
Forward
0 new messages