-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Allow color computations in MathFormula and use to allow computed Turtle colors #530
base: ui2
Are you sure you want to change the base?
Conversation
OK, this now picks up where #528 left off. You can now switch back and forth to the formula display of Turtle parameters, but the formula parameters do not yet have any effect. I will continue the implementation and mark as ready for review when I think all is well, but welcome feedback now or at any point. |
OK, added a colorChooser that will insert into whichever color parameter is currently in effect. The formula parameters are still not implemented. |
Now the list rules convert themselves into formulas whenever they are entered, but Turtle still only draws using the list rules. Also, all of the checks should now pass (at least when run locally). This way, I can switch over to always using the formulas for drawing and make sure that I get exactly the same images. |
OK, Turtle is computing with formulas for all the attributes of each segment: turn, step, width, and color. When you are in List mode and you change the list parameters, it updates all of the formulas to be ones that implement the list rules. I suspect I need to update the Github snapshots, but this can be reviewed in the meantime. And it could really use a Turtle example that actually uses one or more of the formulas in a meaningful way, so Kate if you have or can cook up one or more such example(s), please push a commit that adds it/them Note that the translated formulas for the list rules are pretty klunky; they are done in a way to hopefully work in all circumstances. If you just want the turn angle to be 30 degrees times the sequence value mod 5, for example, you can write |
OK, snapshots updated. All differences were just font/text layout. So the new Turtle is reproducing the old one, while extending it to allow formulas. As far as I am concerned, this just needs a commit with a Featured specimen (or more) that uses formulas, with at least one color formula involved, and the usual review of the PR itself. |
We had a sort of "group review" in meeting today, and it generated the following TODO items; I will mark this PR as draft until they have been implemented:
I am not planning to make any other comments as/when I simply push a commit that implements one or more of the above, but I will check the items off as I do, and mark this again as ready for review when they are all done. |
This produces a disconnected path. This is perhaps a malformed URL in some sense, as it includes both List and Formula mode parameters. To reproduce from first principles, open Wait For It, then go to Formula mode. Change the formula entries to a, a, 1, white; then change the sequence formula to xgcd(3,n)[2]. The resulting path is disconnected, but as far as I can tell from the choices "a,a,1,white" it shouldn't be possible? |
I'm trying to do things like 'chroma.scale()(0.5)' in the colour formula but it doesn't work. |
And this is convergence of the first Riemann zero (approximately....) http://localhost:5173/?name=Convergence+of+the+first+Riemann+zero&viz=Turtle&speed=10&ruleMode=1&turnFormula=log%28n%29*14.134725*360%2F2%2Fpi&stepFormula=200%2Fsqrt%28n%29&colorFormula=chroma%28%27skyblue%27%29.set%28%27hsl.h%27%2C+a%25360%29&seq=Formula&first=10 |
Indeed, it appears that the issue is that the sequence has a negative value, and then the turn formula is raising that to a a fractional power, and so getting a complex number. Then Turtle is trying to interpret that complex number as a turn angle, which it is unable to do. It is producing a popup rather than a formula warning on the parameter because the error is not occurring in the formula computation -- that is working perfectly well to produce a complex number -- but rather in the implementation code itself when it tries to convert the complex number to the JavaScript number type, which is impossible. It's not clear what the best solution here is. The name of the function (that we added to mathjs) "safeNumber" seems to imply that it is always safe to call it. But what is it supposed to do when it is called with something like a matrix or a complex number that mathjs might return, for which there is simply no sensible way to convert the result to a number? I don't see what else it could do but throw an error. So then we would still have to catch errors any time we call safeNumber, which is a pain. So one possibility would be that for formula parameters we could supply a list of allowed return types (as a list of the names that mathjs uses for types) so that the parameter would go into an error state when it returned a disallowed type (and the code would get some value that would forestall further throws). This list could default to |
OK all of the new tests now pass, except for the one that needs code to realize that |
OK, this has percolated and I now have a concrete proposal. MathFormula objects should have a preferred return type, defaulting to bigint (because that's what a Formula Sequence wants), but it can be any type supported by mathjs. The compute infrastructure will attempt to convert to your preferred type for you, so that if the conversion fails, it can return a dummy value and post an error to the status for that formula parameter. How's that sound? I think it provides ease for the visualizer writer while being comprehensible for the sequence explorer -- if the latter puts in a formula that produces an unusable value like a complex number, they will get a message to that effect in the parameter box and can adjust their formula. |
Right, there was a bug that prevented steps of negative length, which are perfectly sensible, from actually being drawn. Fixed now. |
THe difficulty with the built-in color picker is that it operates differently from browser to browser, and there is no reliable way to tell when the explorer closes the color picker. Therefore, this commit switches to using a 3rd-party color picker. The new package already supported a list of colors, so we use that to support a new COLOR_ARRAY param type. The new param type is perfect for Turtle Stroke color(s) parameter in List mode.
OK, it turned out there was literally no way to get an event to reliably fire across browsers when you close the built-in color picker, if the color picked hasn't changed, which mean that with the built-in picker, there was no way to fulfill our goal of re-selecting the same color in the chooser inserting it into the stroke color parameter. So in the latest commit I switched to a 3rd party color picker. That one, it turns out, has a built-in method for selecting a sequence of colors, so I enabled that for the Stroke color(s) parameter. So now there is no need to mess with text representations of colors in the Turtle List mode at all. And therefore the color chooser only appears, right below the Color formula, in formula mode. (The background color button is always active.) Moreover, the chosen color replaces whatever part of the formula you had selected prior to making the choice, making it easy to replace one color in your formula with another. Hopefully this is pleasant and easy to use. I will continue to plow through the many points here. |
Now that the color list is not using text color notations, I recommend we postpone this and not do it as part of this PR. Links in descriptions basically means switching to supporting Markdown descriptions (don't clearly see any other way, since arbitrary HTML is not allowed in the text of fields for security reasons), and that is a larger effort, seems beyond the scope of this PR. If you're comfortable turning on Formula mode, I think we can assume that you are a Numberscope guru or at least know how to find the doc pages... |
Oops, unfortunately this list overlooked that
Any objections/better suggestions, let me know. |
I have filed josdejong/mathjs#3378 to support this notation in mathjs. The "preimplementation" idea looks pretty tricky: it's a small change inside of mathjs's parser, but the parser is open to customization from outside the package only in very specific ways. So it seems as though we would need to basically have a copy of the mathjs parsing code within frontscope in order to "preimplement" this, and it would be temporary until mathjs adopts this or some other notation for allowing function-returning expressions to be called. So my recommendation is to wait on these items: not make them part of this PR, and once a mathjs update is merged that has this function-calling feature, revisit the skipped tests (which now only deal with this point). |
I like the Vertigo of Divergence and am in the process of adding it as a featured specimen. On the other hand, I don't see how your second example reflects the convergence of the first nontrivial zeta zero. If we let Fortunately, that's easy to do: subtracting So what I think is interesting -- definitely something I didn't know before -- is that for larger imaginary parts, at least near roots of zeta, the direct summation of the terms in the series, before it starts its final "death spiral to infinity", comes very near to crossing right over the actual value of its analytic continuation. Moreover, one can easily compute the exact pair of terms such that the midpoint of the line segment between them comes closest to the value of zeta. And you get a pretty picture of what's going on. These statements are the gist of https://math.stackexchange.com/questions/805396/simpler-zeta-zeros that you referenced. So for example http://localhost:5173/?name=Convergence+of+Riemann+Zero+%2310143&viz=Turtle&speed=10&ruleMode=1&turnFormula=log%28n%29*10000.065345417*360%2Ftau+-b&stepFormula=200%2Fsqrt%28n%29&colorFormula=rainbow%282b%29&seq=Formula&first=1&last=3183&length=3183 reproduces exactly the diagram of that math stackexchange article (prettier, though, I think ;-). So we could include that as a featured visualization. There are lots of things to notice here. A few are:
Looking forward to your thoughts/recommendations. One other thing to note is that the alternating sum of inverse s-th powers of n, with the proper scaling factor, does converge in the usual sense to Oh and as far as the third example it was indeed cute but I don't think it's quite "featured specimen" caliber. But I did find the following related specimen that I think could be a candidate: |
If we put an exception into the Turtle code that treats infinite turn angles as 0, then we could instead write the turn formula for the zeta summation at But I am not sure such an exception makes mathematical or logical sense. On the other hand, what else should the poor turtle do with an infinite (or NaN) turn angle? Other tweaks we might make:
|
Yeah, I think that would be useful, because the OEIS includes at least one sequence that lists the turtle bearings for a curve. Sequence A229214 gives the bearings for the flowsnake. It would also be great for drawing cutting sequences on the torus (also known as Sturmian sequences)! |
OK, in progress. When a commit for it is pushed here, I will also post a link for the flowsnake specimen in the interesting specimens discussion. |
@katestange @Vectornaut I am stumped by "TouchEvent is not defined". I cannot reproduce on any browser. So I definitely need more info. I am using Firefox 134.0.2, what version do you have? There are definitely reports of this error online, always associated with either Firefox or Safari, but mostly years old. There is one software package I found (grafana/grafana#78431) that had an issue with this fixed only a year ago, so it could be a thing still. But the funny thing ist neither vue-pick-colors (the component I added) or its sole dependency |
BrowserFirefox 135.0 Reproduction steps
Console messagesWhen I trigger the error, the following log messages appear.
|
Aha! That was really helpful. vue-pick-colors does explicitly mention TouchEvent, it just wasn't showing up in |
OK, @katestange or @Vectornaut, could one of you try the following in this branch:
Sorry about the slightly circuitous route via preview, but I am not clear on how to be certain that Thanks! If this works I will submit a PR with the tiny fixes (that just check if TouchEvent support is enabled before trying to use it; turns out many desktop Firefox distros don't turn on the support and that's what's biting us). |
OK, this isn't really feasible before updating to the new mathjs. It also isn't really germane to this PR; it's just that Kate happened to run into some complex numbers being generated from a negative input to a fractional power while messing around with this PR. So I am going to file this as a separate issue for alpha, so it can be done within or after the mathjs update PR, and take it off the list for this one. |
Oh! It occurs to me that mathjs already has angle units: deg and rad and even the obscure grad (400 grad = τ radians). So I recommend that in formula mode, angles are angles and they should either have a unit, or if they are left unitless, are interpreted as radians. So I think there is no need for a unit toggle (and I prefer not to include one to keep the interface simpler, but I will put a mention in the docs of the units). On the other hand, in List mode, to keep things accessible to newcomers, I suggest we continue to interpret the typed-in numbers as degrees. |
By submitting this PR, I am indicating to the Numberscope maintainers that I have read and understood the contributing guidelines and that this PR follows those guidelines to the best of my knowledge. I have also read the pull request checklist and followed the instructions therein.
This is a reconstituted version of #528. It incorporates the color computations of chroma.js into mathjs and allows them to be used in MathFormulas to compute colors. It then uses this facility to allow the stroke color (and all other segment parameters of Turtle) to be computed by formula.
With the more complicated options for the Turtle parameters, it also adopts the changes to ParamField and ParamEditor layout from the first three commits of #522.