Operator overloading

40 views
Skip to first unread message

Zeger Hendrikse

unread,
Jan 16, 2025, 8:36:48 AMJan 16
to Glowscript Users
Hello everone,

I have just created two extensions to the 3D plot example. I'm trying to make the syntax look even more like Numpy and Matplotlib, and thus I would like to use operator overloading for my own classes. Is it correct that this is currently not possible (yet)?

For those who are interested in the 3D plot extensions:

Bruce Sherwood

unread,
Jan 16, 2025, 2:12:49 PMJan 16
to Glowscript Users
Your programs are beautiful!

What you want to do is in my opinion impossible. Web VPython programs are transpiled to JavaScript in order to be able to run in the browser. JavaScript does not support operator overload. The initiator of VPython, David Scherer, played some major tricks in order to handle operator overload of vectors. 

Consider this tiny program:
a = vec(1,2,3)
b = vec(10, 20, 30)
c = a+b
print(c) # <11, 22, 33) >

If you click "Share or export this program" and scroll through the area at the bottom of the page, you'll see this JavaScript code:

    "2";
    a = vec(1, 2, 3);
    "3";
    b = vec(10, 20, 30);
    "4";
    c = a["+"](b);
    "5";
    print(c);

The Web VPython files that make this work are these:

https://github.com/vpython/glowscript/blob/master/lib/glow/vectors.js

So what one wants is a version of Python that runs in browsers. Steve Spicklemire, who is now the lead developer of VPython, has experimented with the piodyde project that is in fact a version of Python that runs in browsers. He is interested in exploring this approach in more depth. It is my understanding that this version of Python runs signficantly slower than the compiled-to-JavaScript code.

Bruce

Bruce Sherwood

unread,
Jan 16, 2025, 2:44:18 PMJan 16
to Glowscript Users
I should mention that in the generated JavaScript code the numbers such as "3"; are inserted to make it possible to display the program line where an error occurred.

Bruce

Zeger Hendrikse

unread,
Jan 16, 2025, 3:19:48 PMJan 16
to Glowscript Users
Dear Bruce,

First of all thanks for your compliments and your fast response.

You basically confirmed what I already suspected: you have to "hack" the transpilation logic to make operator overloading work.

Currently I'm maintaining two versions of each app, one for the stand-alone VPython, and one that is meant for transpilation to Javascript. This is gradually becoming a maintenance nightmare, so the initiative to migrate to piodyde makes sense to me . On the other hand, with the 3D graphs I've just created, the GPU is working hard already, so the rendering of the mesh may become unacceptably slow when using piodyde. It seems like something has to give eventually ;-)

For now I'll happily stick with my  small Numpy wrapper, as it comes already pretty close to the using the real thing (using the import of the numjs library, which you showed me/us in an earlier post on this forum, by the way).

Thanks again for your fast reply!

Kind regards,

Zeger.

Op donderdag 16 januari 2025 om 20:44:18 UTC+1 schreef Bruce Sherwood:

Zeger Hendrikse

unread,
Jan 16, 2025, 3:25:23 PMJan 16
to Glowscript Users
I forgot to say, coincidently, I'm currently reading Steve Spicklemire's book "Visualizing Quantum Mechanics", and trying to extend/modify the VPython examples presented in his book a bit. I'll give an update when I'm done, but this will take some considerable time, as I have also other things to take care of ;-)

Great book by the way, good to hear he is going to be in charge of VPython!

Bruce Sherwood

unread,
Jan 16, 2025, 4:19:17 PMJan 16
to Glowscript Users
Advertisement: Ruth Chabay and I are the authors of the calculus-based intro physics textbook "Matter & Interactions" (Wiley; matterandinteractions.org). It is the only such textbook that includes a serious introduction to computational modeling of physical systems (this is based on Web VPython). Within a few weeks there will become available a 5th edition, this time adding two more coauthors, Aaron Titus and Steve Spicklemire. We're particularly excited about this because for the first time the ebook will contain embedded interactive Web VPython programs.

Bruce

Zeger Hendrikse

unread,
Jan 16, 2025, 5:34:46 PMJan 16
to Glowscript Users
Perfect timing! I'll keep an eye on that site to see when it has been published.

Op donderdag 16 januari 2025 om 22:19:17 UTC+1 schreef Bruce Sherwood:
Reply all
Reply to author
Forward
0 new messages