dynamic fill-in exercise allowed operations

12 views
Skip to first unread message

Hunter Lehmann

unread,
Jan 21, 2026, 3:38:52 PM (5 days ago) Jan 21
to PreTeXt support
Hi,

I am trying to create a dynamic exercise to compute the Hamming weight of a random vector in F_p^n, say with entries x_1,...,x_6 in {0,1,..p-1}.  Is it possible to use a mod operator somehow in the computation of dynamic values in PreTeXT?

The computation I would like to do is (x_1^4+x_2^4+...+x_6^4) mod 5.  I tried using % to invoke the mod operator but that just breaks the code.

Current exercise code below also.

Best,
Hunter

<exercise xml:id="daily-prep-fp-hamming-weight">
      <setup seed="123456">
        <de-object name="x1" context="number">
          <de-random distribution="discrete" min="0" max="4" by="1" />
        </de-object>
        <de-object name="x2" context="number">
          <de-random distribution="discrete" min="0" max="4" by="1" />
        </de-object>
        <de-object name="x3" context="number">
          <de-random distribution="discrete" min="0" max="4" by="1" />
        </de-object>
        <de-object name="x4" context="number">
          <de-random distribution="discrete" min="0" max="4" by="1" />
        </de-object>
        <de-object name="x5" context="number">
          <de-random distribution="discrete" min="0" max="4" by="1" />
        </de-object>
        <de-object name="x6" context="number">
          <de-random distribution="discrete" min="0" max="4" by="1" />
        </de-object>
        <de-object name="wt" context="number">
          <de-number>(x1^4+x2^4+x3^4+x4^4+x5^4+x6^4)%5</de-number>
        </de-object>
      </setup>
      <statement>
        <p>
          Compute the Hamming weight of <m>(<eval obj="x1"/>,<eval obj="x2"/>,<eval obj="x3"/>,<eval obj="x4"/>,<eval obj="x5"/>,<eval obj="x6"/>)</m>.
        </p>
        <p>
          <fillin mode="math" name="hamming_wt" ansobj="wt" width="4"></fillin>
        </p>
      </statement>
      <evaluation>
        <evaluate name="hamming_wt">
          <test correct="yes">
          <mathcmp obj="wt" />
          </test>
        </evaluate>
      </evaluation>
    </exercise>

D. Brian Walton

unread,
Jan 21, 2026, 9:06:55 PM (4 days ago) Jan 21
to pretext...@googlegroups.com
Hunter,

The mod operator has not been implemented. It could be possible but would require a dependent package is updated and could take some time. Note that the library was designed for floating point arithmetic, and I haven't thought about what happens with the modulus. Also, there is no integer division implemented.

I do need to get back to that and will see about implementing that operator.

Brian

--
You received this message because you are subscribed to the Google Groups "PreTeXt support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pretext-suppo...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/pretext-support/a06d053c-6eca-43e9-9aa9-abc0456b254cn%40googlegroups.com.

Rob Beezer

unread,
Jan 22, 2026, 12:08:43 AM (4 days ago) Jan 22
to pretext...@googlegroups.com
Nothing here should be construed as minimizing Hunter's request.

The possible requests for supported operators is vast. How about polynomial multiplication with coefficients over a finite field? ;-) Yes, I know, out of scope right now.

Years ago, the WeBWorK folks added for me a function to pass computations to Sage and get back the results. It was a bit cumbersome to use (nobody's fault). Suppose there was a dedicated Sage Cell server that FITB problems could utilize? Imagine randomized questions about non-commutative rings for an abstract algebra course.

Not sure what markup, or much else, would look like. Just dreamin'.

Rob
>> <https://groups.google.com/d/msgid/pretext-support/a06d053c-6eca-43e9-9aa9-abc0456b254cn%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
>

D. Brian Walton

unread,
Jan 22, 2026, 5:10:55 PM (3 days ago) Jan 22
to pretext...@googlegroups.com
Hunter,

There was a post to the pretext development list that triggered my memory enough to realize that there IS a solution to your problem, it just requires that you code directly in javascript instead of the expression library markdown. Making a test about the formula as an expression is what I was thinking about in my earlier answer. But the system is already capable of adding arbitrary javascript calculations and tests from the original design — just not many have gone this route.

If you go to this section of the documentation Subsection 4.12.7: Fill-in-the-Blanks Exercises there is a reference to a `jscmp` method that you can use to define your own comparison based on any valid javascript that evaluates to a boolean. Because the modulus operator is a first-class operator in javascript, you could use that for your test if you just want to check a numerical answer for comparison.  It also mentions `setupScript` which allows arbitrary javascript (encapsulated in a function) to generate additional values.

All objects are stored as local variables within an object named `v` for the actual javascript. So having defined your values, instead of defining "wt" as a de-object (where the operator is not defined), you can instead compute "wt" in the setupScript.

<setup>
... same as before except defining wt ...
  <setupScript> <!-- This is pure javascript executed within a function -->
  v.wt = (v.x1^4 + v.x2^4 + v.x3^4 + v.x4^4 + v.x5^4 + v.x6^4) % 5;
  </setupScript>
</setup>

And then instead of <mathcmp> you would try:

<jscmp>answer == wt</jscmp>

(At the comparison step, all of the "v" variables are made 1st class variables in their own right so that we don't need v.{...})

I have not attempted if this works as is, but it should be close. 

Brian
Reply all
Reply to author
Forward
0 new messages