guessing result extraction

17 views
Skip to first unread message

Ralf Hemmecke

unread,
Feb 7, 2025, 6:21:06 AM2/7/25
to fricas-devel
Dear Waldek,

can you explain how to extract the respective formulas from the return
value of Martin's and your guessing package?

====================
%%% (1) -> guess([factorial(4*n)/factorial(n)^4 for n in 1..10])

3 2
n - 1 256 p + 1152 p + 1712 p + 840
++-++ 10 10 10
(1) [24 | | -------------------------------------]
| | 3 2
p = 0 p + 6 p + 12 p + 8
10 10 10 10
====================

(1) How can I manipulate the expression from (1), i.e.
decompose it into its parts (operator, argument, numer, denom)?
(2) How do I get the rational function under the product?
(3) Can I give an option to guess so that the running variable
is returned as k instead of p10?
(4) Is there a demo somewhere or a detailed descriptions also
for the other guessing functions?

Thank you
Ralf

Ralf Hemmecke

unread,
Feb 7, 2025, 6:28:26 AM2/7/25
to fricas...@googlegroups.com
One more question:

(5) Is there an option to let the product begin at p10=1 instead of
p10=0?

Ralf

Waldek Hebisch

unread,
Feb 8, 2025, 5:15:17 PM2/8/25
to 'Ralf Hemmecke' via FriCAS - computer algebra system
On Fri, Feb 07, 2025 at 12:21:01PM +0100, 'Ralf Hemmecke' via FriCAS - computer algebra system wrote:
> Dear Waldek,
>
> can you explain how to extract the respective formulas from the return value
> of Martin's and your guessing package?
>
> ====================
> %%% (1) -> guess([factorial(4*n)/factorial(n)^4 for n in 1..10])
>
> 3 2
> n - 1 256 p + 1152 p + 1712 p + 840
> ++-++ 10 10 10
> (1) [24 | | -------------------------------------]
> | | 3 2
> p = 0 p + 6 p + 12 p + 8
> 10 10 10 10
> ====================
>
> (1) How can I manipulate the expression from (1), i.e.
> decompose it into its parts (operator, argument, numer, denom)?

res := %
k1 := first(kernels(first(res)))
al := argument(k1)

3 2
256 %B + 1152 %B + 1712 %B + 840
(4) [----------------------------------, %B, p , 0, n - 1]
3 2 10
%B + 6 %B + 12 %B + 8
Type: List(Expression(Integer))

Note that above operator is '%defprod', so you just use normal
ways to decompose expressions (that is kernels, operator, argument).

> (2) How do I get the rational function under the product?

eval(al(1), al(2), al(3))

gives you expression, you can coerce it to a rational function.

> (3) Can I give an option to guess so that the running variable
> is returned as k instead of p10?

It seems that you need to 'eval' to get different variable name.
Note that 'guess' can return nested sums or products, so there
may be several running variables.

> (4) Is there a demo somewhere or a detailed descriptions also
> for the other guessing functions?

There is 'mantepse.input', documentation in HyperDoc and an
article in Journal of Symbolic Computation. There are wiki pages:

http://wiki.fricas.org/GuessingFormulasForSequences
http://wiki.fricas.org/SandBoxGuessingSequence

There are messages in the mailing list, yours from 8 Oct 2020,
Martin's from 13 Jan 2009 and 24 Aug 2022 and some other.

You also asked about range of product/summation index. AFAICS
this is fixed. This range is related to range of indices of
the sequence so when changing we probably should change both
(but ATM neither is changable).

--
Waldek Hebisch

Ralf Hemmecke

unread,
Feb 9, 2025, 7:59:33 AM2/9/25
to fricas-devel
Dear Waldek,

thanks a lot for you description for decomposing the result of 'guess'.

I now wanted to rename the running variable by simply modifying the
third entry in the argument list of the %defprod kernel and reconstruct.

%%% (2) -> p := product(x^2+x+1, x=1..n)

n
++-++ 2
(2) | | x + x + 1
| |
x = 1
%%% (3) -> k := kernels(p).1; o := operator k; a := argument k

2
(3) [%B + %B + 1, %B, x, 1, n]
Type: List(Expression(Integer))

%%% (4) -> py := o [a.1,a.2,y,a.4,a.5]

n
++-++ 2
(4) | | x + x + 1
| |
x = 1
Type: Expression(Integer)

Unfortunately, that does not replace x by y. :-(

%%% (4) -> ky := kernels(py).1

n
++-++ 2
(4) | | x + x + 1
| |
x = 1
Type: Kernel(Expression(Integer))

%%% (5) -> oy := operator ky

(5) %defprod
Type: BasicOperator
%%% (6) -> ay := argument ky

2
(6) [%B + %B + 1, %B, x, 1, n]
Type: List(Expression(Integer))

How come that there is an x in this structure?

%%% (8) -> (k=ky)@Boolean

(8) true
Type: Boolean
And why is this true?

The following works, but is certainly not what I want because of a
destructive operation that also modifies the original p.

Ralf

%%% (5) -> a.3 := y

(5) y

%%% (6) -> a

2
(6) [%B + %B + 1, %B, y, 1, n]
Type: List(Expression(Integer))
%%% (7) -> p

n
++-++ 2
(7) | | y + y + 1
| |
y = 1
Type: Expression(Integer)
%%% (8) -> o a

n
++-++ 2
(8) | | y + y + 1
| |
y = 1
Type: Expression(Integer)

Ralf Hemmecke

unread,
Feb 9, 2025, 8:14:51 AM2/9/25
to fricas...@googlegroups.com
Oh, this looks like a problem with the cache. How else would (4) show a
y instead of a z?

Ralf

%%% (1) -> p := product(y^2+1,y=0..n-1)

n - 1
++-++ 2
(1) | | y + 1
| |
y = 0
Type: Expression(Integer)
%%% (2) -> k := kernels(p).1; o := operator k; a := argument k

2
(2) [%B + 1, %B, y, 0, n - 1]
Type: List(Expression(Integer))
%%% (3) -> pol := eval(a.1,a.2=z)

2
(3) z + 1
Type: Expression(Integer)
%%% (4) -> product(pol,z=0..n-1)

n - 1
++-++ 2
(4) | | y + 1
| |
y = 0
Type: Expression(Integer)
%%% (5) -> product(pol,z=0..n)

n
++-++ 2
(5) | | z + 1
| |
z = 0
Type: Expression(Integer)

Waldek Hebisch

unread,
Feb 9, 2025, 8:35:40 AM2/9/25
to 'Ralf Hemmecke' via FriCAS - computer algebra system
On Sun, Feb 09, 2025 at 01:59:29PM +0100, 'Ralf Hemmecke' via FriCAS - computer algebra system wrote:
> Dear Waldek,
>
> thanks a lot for you description for decomposing the result of 'guess'.
>
> I now wanted to rename the running variable by simply modifying the third
> entry in the argument list of the %defprod kernel and reconstruct.
>
> %%% (2) -> p := product(x^2+x+1, x=1..n)
>
> n
> ++-++ 2
> (2) | | x + x + 1
> | |
> x = 1
> %%% (3) -> k := kernels(p).1; o := operator k; a := argument k
>
> 2
> (3) [%B + %B + 1, %B, x, 1, n]
> Type: List(Expression(Integer))
>
> %%% (4) -> py := o [a.1,a.2,y,a.4,a.5]
>
> n
> ++-++ 2
> (4) | | x + x + 1
> | |
> x = 1
> Type: Expression(Integer)
>
> Unfortunately, that does not replace x by y. :-(

This is design choice in current Expression. Consider:

(1) -> product(i, i=1..n)

n
++-++
(1) | | i
| |
i = 1
Type: Expression(Integer)
(2) -> product(i, i=1..n) - product(j, j=1..n)

(2) 0
Type: Expression(Integer)
(3) -> product(j, j=1..n)

n
++-++
(3) | | i
| |
i = 1
Type: Expression(Integer)

(2) is obtained thanks to (3). More precisely, when creating new
kernel FriCAS tests if it is equal to one of the already existing
kernels and in such case returns the old one. In case of sums and
products FriCAS knows that indices are "dummies" and their names
do not take part in comparison.

If you want different behaviour, there is now KernelCategory,
you should be able to define new kind of kernels which behaves
differently. But a lot of things in algebra depend on current
behaviour of Kernel, so that is not an easy change.

Alternatively, there were few attempts to create "symbolic"
domains, you could use any of them to represent more general
expressions, possibly converting to/from Expression to
perform actual computations.

> The following works, but is certainly not what I want because of a
> destructive operation that also modifies the original p.
>
> Ralf
>
> %%% (5) -> a.3 := y
>
> (5) y
>
> %%% (6) -> a
>
> 2
> (6) [%B + %B + 1, %B, y, 1, n]
> Type: List(Expression(Integer))
> %%% (7) -> p
>
> n
> ++-++ 2
> (7) | | y + y + 1
> | |
> y = 1
> Type: Expression(Integer)
> %%% (8) -> o a
>
> n
> ++-++ 2
> (8) | | y + y + 1
> | |
> y = 1
> Type: Expression(Integer)

That is problematic, kernel cache should be sorted, but modification
changes ordering relation. You may get "lucky" because some cases
revert to linear search.

--
Waldek Hebisch

Ralf Hemmecke

unread,
Feb 9, 2025, 9:42:20 AM2/9/25
to fricas...@googlegroups.com
>> Unfortunately, that does not replace x by y. :-(

> This is design choice in current Expression.

Well, OK, I accept that the third argument is irrelevant for equality.
But it's also a bit unfortunate, since some people like to see "nice"
output. I thought that the third parameter was exactly supposed to make
that heppen, but now the caching system comes into the way. :-(
Then the third parameter is basically useless and only helps to hide the
%A kind of variables.

Is there a way to print a sum/product with a pre-specified running
variable if a different one is already in the cache or do I really have
to program such a printing function myself?

> If you want different behaviour, there is now KernelCategory,
> you should be able to define new kind of kernels which behaves
> differently.

Do I understand correctly, that one way would be to create MyKernel(X)
and then coerce the expression into MyKernel and print it.

I guess that would not satisfy my if I have sum(....)*sum(....) since
then I end up in Expression(...) and therefore again in Kernel (not
MyKernel.

Can I perhaps delete a specific kernel from the Cache?

> Alternatively, there were few attempts to create "symbolic"
> domains, you could use any of them to represent more general
> expressions, possibly converting to/from Expression to
> perform actual computations.

Oh, what are these "symbolic" domains in FriCAS?

Ralf

Waldek Hebisch

unread,
Feb 9, 2025, 10:45:39 AM2/9/25
to 'Ralf Hemmecke' via FriCAS - computer algebra system
On Sun, Feb 09, 2025 at 03:42:16PM +0100, 'Ralf Hemmecke' via FriCAS - computer algebra system wrote:
> > > Unfortunately, that does not replace x by y. :-(
>
> > This is design choice in current Expression.
>
> Well, OK, I accept that the third argument is irrelevant for equality.
> But it's also a bit unfortunate, since some people like to see "nice"
> output. I thought that the third parameter was exactly supposed to make that
> heppen, but now the caching system comes into the way. :-(
> Then the third parameter is basically useless and only helps to hide the %A
> kind of variables.

Yes, there is a problem.

> Is there a way to print a sum/product with a pre-specified running variable
> if a different one is already in the cache or do I really have to program
> such a printing function myself?

Currently there is no special support. Some time ago I wrote about
printing of variables in SparseUnivariatePolynomial. Current issue
with dummy variables have some similarity. In both cases it would
be desirable to have "context dependent" output form, but currently
nothing of this included in FriCAS. In fact, also roots have
similar problem: where FriCAS creates algebraic kernel it gets
name, either user specified or assigned automatically. Once there
is name all equal kernels will get this name.

Extra thing: if there are no name clashes one could do substitution
in OutputForm using Lisp functions (it would be easy to add a wrapper
so that this is available at Spad level). I was concerned with
case when there are clashes, is such case one needs to do more.

Also, I wanted this as part of output machinery (expression may be
"lost" after output, so later transformation may be too late).

Thinking more: currently probably simplest solution for you is to
add new operator(s), like product but without dummy variables.

> > If you want different behaviour, there is now KernelCategory,
> > you should be able to define new kind of kernels which behaves
> > differently.
>
> Do I understand correctly, that one way would be to create MyKernel(X)
> and then coerce the expression into MyKernel and print it.

Well, MyExpression based on MyKernel. MyExpression now can be a
FunctionSpace2. There is still work to make FunctionSpace2
acceptable in places where we can use FunctionSpace. Ideally
Expression would be more general allowing MyKernel intead of
Kernel.

> I guess that would not satisfy my if I have sum(....)*sum(....) since then I
> end up in Expression(...) and therefore again in Kernel (not MyKernel.
>
> Can I perhaps delete a specific kernel from the Cache?

This in not really feasible. You could try to delete whole
cache.

> > Alternatively, there were few attempts to create "symbolic"
> > domains, you could use any of them to represent more general
> > expressions, possibly converting to/from Expression to
> > perform actual computations.
>
> Oh, what are these "symbolic" domains in FriCAS?

Nothing is included in the trunk. Tim Daly long ago posted IIRC
'CohenAlgebra'. Bill Page posted a "symbolic algebra" (based on
InputForm). I created 'IntegerAsNothing' (basically applying
forgetful functor to Integer) and used 'Expression(IntegerAsNothing)'.
I needed pattern matching, so I made a few modifications to Expression
and pattern matching. I probably send part or all of this to
the mailing list, but IIRC nothing is included in current
FriCAS.

--
Waldek Hebisch
Reply all
Reply to author
Forward
0 new messages