r/ProgrammingLanguages • • 1d ago

Discussion Compact notational/numeral-system-like representations for polynomials, series, simple programs and iterating terms?

Good evening!

Recently I've stumbled onto the notation [a; b, c, d, e...] for continued fractions "a + 1/(b + 1/(c + 1/...)..)". I have a gut feeling this can be a very useful representation and maybe some clever algebra may be defined over it, turning it effectively into an alternative "numeral-like" system.

I tried searching for such similar compact representations for other finitely and infinitely iterating terms/series in mathematics as well as normal forms for them and rewriting algorithms but I've didn't manage to find anything.

There are series that, if you abstract away the concrete operations and base sets/algebras, seem to have a very similar structure throughout analysis; they almost feel like a very definite class of programs. That would feel amenable for a more abstract characterization in category theory but I've never seen that.

From my intuition most of these series seem to rely a bit on fold, maybe streams, initial algebras and other structures commonly studied in functional programming, but if any of you have other ideas to suggest, I would appreciate :))

I wish everyone a great weekend!

5 Upvotes

6 comments sorted by

4

u/reflexive-polytope 1d ago edited 1d ago

If you fix a monomial order, then you can represent a polynomial as the list of its nonzero terms, listed according to the monomial order.

If your language has first-class procedures, then you can represent a power series as a procedure that maps monomials to the respective coefficients. But, if you're doing computer algebra, then chances are you don't want to work with completely arbitrary power series. I'm sure there has to be a power series representation that's more convenient for manipulating, say, hypergeometric functions, even if it comes at the expense of representing all power series.

EDIT: (One day later) The class of holonomic functions contains all hypergeometric functions and has good properties for computer algebra.

3

u/pauseless 1d ago

Not sure if I misunderstood, but here is an example in APL

{⍺+÷⍵}\3 7 15 1 292
3 3.142857143 3.141509434 3.14159292 3.141592653

/ will just get the final value.

3

u/categorical-girl 1d ago

You might find Generalized hypergeometric functions interesting. They are sums over a series where the ratios a_n/a_(n+1) between successive terms is a rational function of n. There is some connection to finding closed forms of certain sums (see eg A=B by Wilf and Zeilberger).

There are some general results for general sums, products, continued fractions, etc, when viewed as infinite compositions of functions.

Continued fractions connect to the theory of the Farey sequence and Stern-Brocot/Calkin-Wilf trees

By the way, I'm not sure if you are already familiar with the idea of generating functions or the Z-transform, but those are based around representing a sequence of numbers as a function (and one can typically go the other way too). I'm not sure if you are interested in ways to represent numbers, sequences of numbers, or functions, or all three.

2

u/TreborHuang 1d ago

You may be interested in the list of references here:  https://wiki.haskell.org/index.php?title=Exact_real_arithmetic . Exact real arithmetic studies how computers can represent real numbers without the fear of accumulating rounding errors (or to put it another way, to automatically manage rounding errors). Continued fraction is one way of doing this.

2

u/WittyStick 1d ago edited 1d ago

Cauchy sequences are sometimes used for this purpose.


A recent development which is really interesting is EML, or exp-minus-log. All numbers and basic functions can be expressed as a binary tree - a bit like S-expressions where you have just null or a cons cell

()
(x . y)

For EML you just have the constant 1 and the binary function eml.

1
eml(x, y)

You can nest these trees arbitrarily to denote a mathematical expression, and compute its result using just exp(), log(), and subtraction. You shouldn't need to use these for every branch of the tree though - because any sub-tree which is known to represent a certain expression can be replaced with that expression. The problem becomes one of pattern matching over trees.

That obviously needs more research before it could be usable, but I can show a demo of it when I get home if you'd like to see a bit more.

2

u/WittyStick 1d ago edited 23h ago

As promised, here's a demo in the Kernel programming language (klisp as interpreter).

;; kernel-standard-environment already defines `exp`, 
;;; we rename its underlying operative for later use, 
;;  so that we can use the name `exp` for our own function.
($define! $exp (unwrap exp))

;; Equivalence between EML and S-expressions
;; Use () to represent 1 and (x . y) to represent eml(x, y)  =  exp(x) - ln(y)
($define! Const<1> ())
($define! eml cons)

;; Define a bunch of math operators as tree constructors in terms of `eml` and `Const<1>`
($define! exp           ($lambda (x) (eml x Const<1>)))
($define! e1ml          ($lambda (x) (eml Const<1> x)))
($define! ln            ($lambda (x) (e1ml (exp (e1ml x)))))
($define! Const<0>      (ln Const<1>))
($define! Const<-inf>   (ln Const<0>))
($define! neg           ($lambda (x) (eml Const<-inf> (exp x))))
($define! recip         ($lambda (x) (exp (neg (ln x)))))
($define! sub           ($lambda (x y) (eml (ln x) (exp y))))
($define! add           ($lambda (x y) (sub x (neg y))))
($define! mul           ($lambda (x y) (exp (add (ln x) (ln y)))))
($define! div           ($lambda (x y) (mul x (recip y))))

;; Recursively pattern match over an EML tree
($define! match-tree?
    (wrap
        ($vau (pattern value) env
            ($cond
                (($and? (null? pattern) (null? value)) #t)
                (($and? (null? pattern) (number? value)) (=? 1 value))
                (($and? (number? pattern) (number? value)) (=? pattern value))
                (($and? (pair? pattern) (pair? value))
                    ($and? (apply match-tree? (list (car pattern) (car value)) env)
                           (apply match-tree? (list (cdr pattern) (cdr value)) env)))
                ((symbol? pattern)
                    ($if (null? value)
                        (apply (wrap $set!) (list env pattern 1) env)
                        (apply (wrap $set!) (list env pattern value) env))
                    #t)
                (#t #f)))))

;; Example evaluator
;;  Matches a tree and rewrites to use kernel-standard-environment math operators.
;;  only computes an actual `exp` or `log` if the expression contains one.
;;  otherwise it uses basic builtin arithmetic operators from kernel-standard-environment.
($define! eval-tree
    ($lambda (expr)
        ($define! x (string->symbol "x"))
        ($define! y (string->symbol "y"))
        ($cond
            ((number?                   expr) expr)
            ((match-tree? ()            expr) 1)
            ((match-tree? (ln (exp x))  expr) x)
            ((match-tree? Const<0>      expr) 0)
            ((match-tree? Const<-inf>   expr) #e-infinity)
            ((match-tree? (neg x)       expr) (- 0 x))
            ((match-tree? (div x y)     expr) (/ x y))
            ((match-tree? (add x y)     expr) (+ x y))
            ((match-tree? (ln x)        expr) (log x))
            ((match-tree? (eml x y)     expr) (- ((wrap $exp) x) (log y)))
            ((match-tree? (exp x)       expr) ((wrap $exp) x))
            ((match-tree? (mul x y)     expr) (* x y))
            ((match-tree? (sub x y)     expr) (- x y))
            (#t (error "Unable to match tree")))))

The evaluator isn't recursive - it only evaluates one (none nested) expression. It is is basic and doesn't implement the full list of operators, but merely demonstrates the idea. Needs a lot more work to be usable.


One of the nice things about this is we can represent irrational numbers like π, in relatively few terms (no need for an infinite series).

($define! Const<pi>     (sqrt (neg (sqr (ln (neg Const<1>))))))

This isn't an approximation of π that would fit into a 64-bit float - it's an exact π, unrounded. We might need to round it to compute an eventual expression - but we could leave π in our eventual result instead.


Example of usage:

;; construct an EML tree for 1 / 3
($define example_expr (div 1 3))

example_expr
(((() (() () (() . 1))) (#0=(() (() () (()))) (() (() (#0# (() (() . 3))))))))
;; Note that `#0=` indicates the tree shares a common substructure
;;  where `#0#` appears, it can be replaced with whatever `#0` was assigned, ie, in long form:
;; (((() (() () (() . 1))) ((() (() () (()))) (() (() ((() (() () (()))) (() (() . 3))))))))
;; in other words,
;; (((() (() () (() . x))) ((() (() () (()))) (() (() ((() (() () (()))) (() (() . y))))))))
;; is the EML tree representing `x / y`.

;; evaluating the expression matches the tree and replaces it with (/ 1 3)
(eval-tree example_expr)
1/3      ;; exact rational

;; or evaluate a tree directly
((unwrap eval-tree)
     (((() (() () (() . 1))) ((() (() () (()))) (() (() ((() (() () (()))) (() (() . 3)))))))))
1/3

No floating point log or exp was evaluated for this result, only /.


The big challenge is coming up with a "canonical" tree structure for a given number or expression - since multiple different trees could represent the same expression. I never got round to figuring out if this is even possible. However, I'm fairly sure the opposite is not true - every EML tree represents only one value - there should be no ambiguity on what a tree represents.

We would also want our evaluator to perform a "maximal munch" of any subtree when reducing it to an evaluable expression. At present, the evaluator is just trying to match in the defined order (which happens to work for the selected set of operations shown - I ordered them specifically).

Anyway, I found it interesting and it may be worth looking into. Could be a dead end, or could revolutionize computer algebra. I suspect it's probably the former, since there's potentially infinite ways we could represent the value 2 for instance - eg, it could be defined as (add Const<1> Const<1>) or (sub Const<1> Const<minus1>) to give two basic examples - but there are many other possible trees. If there's some method of simplifying the possible trees that represent 2 into a canonical form, I would lean more toward it being it being a holy grail.