Ask Your Question

Boston's profile - activity

2020-07-16 07:05:28 -0600 commented question Built-in tools for fast extraction of tuples of a given grade with respect to grading on [1, 2, ..., n]

@mwageringel Thanks a lot :-)

2020-07-12 12:37:33 -0600 asked a question Built-in tools for fast extraction of tuples of a given grade with respect to grading on [1, 2, ..., n]


Note that I have just posted the same question on Stack Overflow.

I am in the process of coding 2 mathematical combinatorial operations that I want to iterate (as) many times (as possible), so speed is obviously crucial.

Here is an example of what I want to do as fast and efficiently as possible for any positive integer n and any grading (see example below).

Remark: This is a self-motivated question coming from mathematical experimentations with Sage. Many thanks in advance for your help.

Example: Let us take n = 6, and the set S = {1, 2, ..., n} = {1, 2, 3, 4, 5, 6}. There are binomial(6, 2) = 15 couples

[(1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (2, 3), (2, 4), (2, 5), (2, 6), (3, 4), (3, 5), (3, 6), (4, 5), (4, 6), (5, 6)]

in increasing order that we can extract from S. The built-in tool I was advised to use in order to obtain the above list is the following:

from itertools import combinations n = 6 iterable = list(range(1, n+1)) r = 2 print(list(combinations(iterable, r)))from itertools

Question 0: Is there anything faster in Python?

Now let me choose a grading on the set S in the form of a function from S to {1, 2, 3}

grading: 1, 2, 3 --> 1 and 4, 5 --> 2 and 6 --> 3

I have decided to store this function as a dictionary as follows:

grading = {1: 1, 2:1, 3: 1, 4: 2, 5: 2, 6: 3}

With respect to this grading, we can compute the grade of elements, couples, or tuples constructed from S.

Examples: - grade(2) = 1 - grade(5) = 2 - grade((2, 5)) = grade(2) + grade(5) = 1 + 2 = 3 - grade((2, 3, 5)) = 1 + 1 + 2 = 5

1 - First Construction:

I want all the couples with grade equal to g0 = 4. Here is an obvious way to do it, which is built naively on the previous lines of code:

g0 = 4 gradedList = [] for couple in list(combinations(iterable, 2)): grade = grading[couple[0]] + grading[couple[1]] if grade == g0: gradedList.append(couple) print(gradedList)

This yields

[(1, 6), (2, 6), (3, 6), (4, 5)]

as desired.

Question 1: Is there a built-in way to obtain gradedList and/or what is the fastest way to do this with Python?

2 - Second Construction:

Now I want to extract all the increasing 3-tuples (i, j, k) with grade equal to 4, and which starts with i = 1.

In other terms, I want the following list:

[(1, 2, 4), (1, 2, 5), (1, 3, 4), (1, 3, 5)]

Of course, this can be obtained as follows:

newIterable = list(range(2, n+1)) secondGradedList = [] for couple in list(combinations(newIterable, 2)): grade = grading[1] + grading[couple[0]] + grading[couple[1]] if grade == g0: secondGradedList.append((1,) + couple) print(secondGradedList)

Question 2: Is there a built-in way to obtain secondGradedList and/or what is the fastest way to do this with Python?

Thanks, Julien

2020-07-03 07:32:53 -0600 asked a question Right choice for a dictionary of matrices with non constant coefficients.


K is any field.

I need to store matrices in a dictionary.

Some of them have constant coefficients in K (let M1 be such a matrix), and some have non constant coefficients, say these coeffcients are functions from t to K (let M2 be such a matrix).

I need to get M2 from the dictionary, and then be able to manipulate it as if it was a function of t.

(T) In particular, I need to be able to manipulate the matrices obtained from M2 by setting t to be equal to some specific values.

Since my functions are polynomials for now, I have tried doing something like this:

var('t') sample = {} sample[('M1')] = matrix(QQ, 2, [7, 5, 2, 4]) sample[('M2')] = matrix(QQ['t'], 2, [t^2, 2, 3, 1 - t])

But then I don't know how to proceed to perform the task (T). And I have the feeling that there are much better options.

What should I do?


2020-05-27 15:26:56 -0600 commented question Matrix of the composition with the quotient map

@tmonteil An alternative would be to know how to obtain the coordinates of every $\theta(u)$ in the basis of Q, knowing the coordinates of $\phi(u)$ in the canonical basis of $\mathbb{C}^4$.

2020-05-27 14:03:57 -0600 commented question Matrix of the composition with the quotient map

I have a basis of U and I am able to compute $\phi(u)$ for every $u$ in this basis. So I build the matrix M myself: this is now my input. I am looking for the shortest path to get the matrix N.

2020-05-27 08:54:24 -0600 asked a question Hypermatrices?

Hi all,

I want to store and manipulate arrays of $n\times n\times n$ numbers $$x_{ijk}\quad\text{where}\quad 0\leq i, j, k\leq n-1$$ with Sage.

Here is what I have been doing so far:

import numpy as np

n = 7

X = np.zeros((n, n, n))

X[2][4][1] = 17


Remark: So $X$ is a so called 3-hypermatrix.

Question: Are NumPy arrays my best option when handling hypermatrices with Sage?

Precision: I will need to manipulate lists of such hypermatrices, and even hypermatrices whose elements will be hypermatrices, e.g. $$X_{ijk}^{(abcd)}$$ and loop over such lists, e.g. for a ..., for b ..., for c ..., do ... with the 3-hypermatrix $X^{(abcd)}$.

2020-05-27 08:01:38 -0600 asked a question Matrix of the composition with the quotient map

Hi all,

In this question, I learned how to obtain a basis for a quotient vector space.

Here is a summary of the code:

A = matrix(CC, 4, 3, [1, 2, 3, 5, 10, 15, 0, 0, 0, -2, -4, -6])

B = matrix(CC, 5, 4, [0, 2, 7, 5, 0, 0, 0, 0, 0, -2, -7, -5, 0, 4, 14, 10, 0, 6, 21, 15])

W = A.transpose().image()

V = B.right_kernel()

Q = V / W

So Q is the quotient vector space $\mathrm{Ker}B/\mathrm{Im}A$ and the command

matrix([Q.lift(b) for b in Q.basis()])

returns a matrix whose rows are vectors of $\mathbb{C}^4$ which constitute a set of representatives for a basis of Q.

Now I have a linear map $$\phi:U\longrightarrow\mathbb{C}^4$$ such that $\mathrm{Im}\phi\subseteq V$, and its composition with the quotient map $V\longrightarrow Q=V/W$, namely $$\theta:U\longrightarrow Q.$$ I have fixed a basis of $U$ (which has dimension 3) and I have the matrix of $\phi$, say $M$, with respect to that basis at the origin, and the canonical basis of $\mathbb{C}^4$ at the end. So $M$ is $4\times 3$.

Question: What is the command that will give me the $2\times 3$ matrix $N$ of $\theta$ with the same basis of $U$ at the origin, and the basis of $Q$ mentioned above at the end?

Thanks for your help.

2020-05-25 14:25:50 -0600 commented answer Basis for such a quotient vector space

Great, thanks a lot.

2020-05-25 12:26:58 -0600 asked a question Basis for such a quotient vector space

Hi all,

Here is my code:

A = matrix(QQ, 4, 3, [1, 2, 3, 5, 10, 15, 0, 0, 0, -2, -4, -6])

B = matrix(QQ, 5, 4, [0, 2, 7, 5, 0, 0, 0, 0, 0, -2, -7, -5, 0, 4, 14, 10, 0, 6, 21, 15])

W = A.transpose().image()

V = B.right_kernel()


This way W is the range of the linear map $$A:\mathbb{Q}^3\longrightarrow\mathbb{Q}^4,$$ V is the nullspace of $$B:\mathbb{Q}^4\longrightarrow\mathbb{Q}^5,$$ Since BA=0, W is a subspace of V. and the command V/W returns in particular the dimension of the quotient vector space V/W which is equal to 2.

What I want is a basis of V/W, i.e. 2 vectors of $\mathbb{Q}^4$ which are in V, in the form of a 2x4 matrix the same way V.basis_matrix() returns a basis for V.

How can I do this?


2019-12-25 11:38:45 -0600 received badge  Editor (source)
2019-12-25 11:05:00 -0600 asked a question error message: <input>:2: DeprecationWarning: invalid escape sequence \(


I have a few interacts which were working nicely until this morning. But now I get this error message:

<input>:2: DeprecationWarning: invalid escape sequence \(

Here is an example:

Can you tell me what is wrong, and what I should do to fix it?

Thanks! Julien

2019-09-06 11:14:08 -0600 received badge  Famous Question (source)
2018-12-30 13:02:09 -0600 received badge  Notable Question (source)
2018-09-08 18:15:47 -0600 received badge  Popular Question (source)
2018-03-26 12:53:47 -0600 asked a question Using accented letters in interact selectors

I would like to use accented letters when displaying the options of my input under the selector method. To be more precise, 'I'd like to have ellipsoïde and à deux nappes, for instance. Thanks.

Here is the interact. And here is the code:

quadrics = {'Ellipsoide':x^2+y^2+z^2-1,'Paraboloide elliptique':x^2+y^2-z,'Paraboloide hyperbolique':x^2-y^2-z, 'Hyperboloide une nappe':x^2+y^2-z^2-1,'Hyperboloide deux nappes':x^2-y^2-z^2-1, 'Cone':x^2+y^2-z^2}
def quads(q = selector(quadrics.keys(), label='Quadrique'), a = input_box(default=1, label="$a$"), b = input_box(default=1, label="$b$"), c = input_box(default=1, label="$c$"), r = input_box(default=3, label="$r$")):
f = quadrics[q].subs({x:x/a}, {y:y/b}, {z:z/c})
if a*b*c==0 or q=='Cone':
    pretty_print(latex(f)+'=0 '+'(dégénérée)')
p = implicit_plot3d(f,(x,-r,r),(y,-r,r),(z,-r,r), plot_points = 75, mesh=1, opacity=.7)
2018-01-29 08:11:21 -0600 received badge  Nice Question (source)
2018-01-28 16:20:47 -0600 asked a question plots of complex numbers

Say I want to plot the 6 solutions of the following complex equation.

What is the best practice?

EDIT by @tmonteil: to lower dependency between Sage services in the long term, here is the code provided in the sagecell:

solutions  = solve (z^6==-8, z)
for i in range(0,6):
2018-01-27 12:50:50 -0600 received badge  Supporter (source)
2018-01-25 16:31:39 -0600 asked a question pretty_print in interact labels

I have written the following interact.

How cant I get the labels right? For instance, I'd rather have $N_0$ rather than N0.

Can I achieve this with pretty_print?

2018-01-19 14:11:30 -0600 commented answer @interact : Fix the default rendering

Thanks kcrisman!

2018-01-19 14:11:04 -0600 received badge  Scholar (source)
2018-01-18 09:45:46 -0600 asked a question @interact : Fix the default rendering

I have created the following interact.

It plots the partial sums of a series up to N, et prints out the last one.

The default rendering yields a DeprecationWarning and does not show the partial sum value. When you update the data, the rendering is fine.

How can I fix this?

2015-11-06 19:14:25 -0600 received badge  Famous Question (source)
2015-06-19 18:28:21 -0600 received badge  Notable Question (source)
2015-06-19 18:28:21 -0600 received badge  Popular Question (source)
2014-08-11 10:05:38 -0600 received badge  Student (source)
2014-08-08 10:42:03 -0600 asked a question How to reset variables?

I am using the Sage-6.2 app on an iMac (Mavericks).

I have solved a couple of linear systems in my running worksheet for which the solution set requires a parameter. I am now at 'r7'.

How do I reset this back to 'r1'?

Also, is there something equivalent to Maple's 'restart'?