Ask Your Question

Spirit's profile - activity

2021-11-08 16:00:50 +0200 marked best answer Use expression.find with SR.wild in python scripts

I want to detect polynomials of the form x^n + m in a python script.

Found this helpful piece of code that works perfectly in sage's jupyter notebook:

x = var('x')
w0 = SR.wild(0)
w1 = SR.wild(1)
(x**2-2).find(x**w0+w1)

However, when I throw this into a .py file and run it I get the error *** TypeError: unsupported operand parent(s) for ^: 'Symbolic Ring' and 'Symbolic Ring'

Minimal Failing Example:

from sage.all import *
import sage
from sage.calculus.var import var

from sage.symbolic.ring import SymbolicRing
SR = SymbolicRing()
polynomial = SR('x^2-2')

x = var('x')
w0 = SR.wild(0)
w1 = SR.wild(1)
polynomial.find(x**w0+w1)

What am I missing?

2021-10-27 16:21:11 +0200 asked a question Use expression.find with SR.wild in python scripts

Use expression.find with SR.wild in python scripts I want to detect polynomials of the form x^n + m in a python script.

2021-02-24 15:00:45 +0200 received badge  Supporter (source)
2021-02-24 15:00:36 +0200 received badge  Scholar (source)
2021-02-01 20:52:04 +0200 received badge  Student (source)
2021-02-01 13:22:06 +0200 received badge  Editor (source)
2021-02-01 11:17:11 +0200 asked a question Use vector as variable for diff but as values for calculation

I am implementing the Squared Exponential kernel in SageMath, for now let's say it's defined as follows:

$f(x_i,x_k)=σ^2 \exp\left(−\frac{1}{2 \ell^2} \sum_{j=1}^q (x_{i,j} − x_{k,j})^2 \right)$

With $x_i$ and $x_k$ vectors of variable, but equal, length, $\sigma$ and $l$ constant. (In the future $l$ might be vector valued as well, I hope this can be handled then).

The function $f$ must be differentiable in $x_i$ and $x_k$ (not its entries!).
An example implementation might look as follows:

n = 8
x1 = vector(list(var('v1_%d' % i) for i in range(1, n+1)))
x2 = vector(list(var('v2_%d' % i) for i in range(1, n+1)))
sigma,l = var('sigma, l')
f = sigma*e^(
    sum(vector([(n - m)^2 for n, m in zip(x1.coefficients(), x2.coefficients())]))
    *(-1/(2*length_scale)))

But what I actually want is this:

f(x1, x2) = sigma*e^(sum((x1[i]-x2[i])^2, i, 0, n))*(-1/(2*length_scale)))

So that, hopefully, I can do f.diff(x1) and it considers the sum etc. properly. The problem is that sum needs a variable, but x1[i] demands that i is an integer, so it can grab the elements from the vector.

Is there any obvious method I have missed, or is it not implemented in sage yet? -> If there is no such method, what would be good starting points for me to implement that myself?

Notes: For later processing I need the function to be a differentiable SageMath expression instead of e.g. a Python-function call. Further, it would be great (but not 100% required) to pass variable length vectors to $f$ instead of specifying the length beforehand.