Ask Your Question

DanialBagh's profile - activity

2019-12-12 10:46:24 -0500 received badge  Popular Question (source)
2019-08-26 08:36:13 -0500 received badge  Famous Question (source)
2019-08-26 08:36:13 -0500 received badge  Notable Question (source)
2019-01-17 17:09:31 -0500 received badge  Popular Question (source)
2018-10-11 01:31:21 -0500 commented answer Numerical calculation speed: Sagemath vs. C vs. Matlab

Thanks. I think that is meant for very short code (which is a good thing for some situations).

2018-10-10 14:31:52 -0500 commented answer Numerical calculation speed: Sagemath vs. C vs. Matlab

Thank you for the answer. I will look up some Numpy benchmarks. I might even write the numerical part in plain python without Sagemath, but I will probably run into problems with regard to lack of linear algebra packages there. But my aim is to distribute the resulting software in a small program. Relying on Sagemath will need the 2-Gig installation.

2018-10-10 14:26:53 -0500 received badge  Commentator
2018-10-10 14:26:53 -0500 commented answer Numerical calculation speed: Sagemath vs. C vs. Matlab

Thank you for the very detailed response. I am trying write a research paper and I want to use free software for doing it, and I think Python based Sagemath Jupyter is gaining traction within the scientific community (did you hear that the economy nobel price winner of this year uses it?). It would benefit me to streamline all parts (both the symbolic computations and the numerical part) in one sheet. Thank you answering the question I was too shy to ask: how to force Sagemath to stop treating numbers as symbolic and just do regular double-precision calculations. I will post the feedback if I did end up writing it in Sagemath instead of taking numerics to a separate C program. Thanks again.

2018-10-10 02:23:47 -0500 asked a question Speed of numerical calculation: Sagemath vs. C vs. Matlab

I mostly use Sagemath for performing symbolic operations on trigonometric polynomials. But I was wondering if it is also a good tool in numerical methods. This includes matrix operations and iterative solution of PDEs with finite difference methods. Usually I take the Sagemath outputs to another program more suited to numerical work. But I was wondering if Sagemath itself is a viable choice in this resepct. Can you tell me about its numerical performance and speed compared to other programs such as C, Matlab, Julia, Mathematica etc.? Thanks

2018-10-10 02:22:46 -0500 asked a question Numerical calculation speed: Sagemath vs. C vs. Matlab

I mostly use Sagemath for performing symbolic operations on trigonometric polynomials. But I was wondering if it is also a good tool in numerical methods. This includes matrix operations and iterative solution of PDEs with finite difference methods. Usually I take the Sagemath outputs in another program more suited to numerical work. But I was wondering if Sagemath itself is a viable choice in this resepct. Can you tell me about its numerical performance and speed compared to other programs such as C, Matlab, Julia, Mathematica etc.? Thanks

2018-10-06 04:14:19 -0500 marked best answer How to get coefficients of a multivariate trigonometric polynomial?

I have a trigonometric polynomial which is something like this:

A = (b1+2) * sin(x) + (b2+q) * sin(x)^2cos(x) + (b3/b1+6) * sin(x)cos(x) + b4 * cos(x)^4 + b5

I want to get the coefficients of the multivariate polynomial with respect to sin(x1) and cos(x1). Please note that all other variables should be treated as coefficients, including b1, b2, b3, b4, b5 and q. I would rather do this with coefficient() command, but my only problem is that coefficient command works for univariate polynomial. I can write:

A.coefficients(sin(x))

But I can't write A.coefficients({sin(x),cos(x)}) because it given an error. What can I do?

The answer should produce something like this: coefficient,(exponent of sin(x),exponent of cos(x)); b1+1,(1,0); b2+q,(2,1); b3/b1+6,(1,1); b4,(0,1); b5,(0,0).

2018-10-06 04:14:16 -0500 marked best answer get the coefficients of polynomial of several variables?

Consider that I have a polynomial with n variables x1,x2,...,xn and want to get the coefficient of that polynomial. For example I can have: x1+2x1x5^2+3x1^2x4-5x2x5^2+1/2x2x3+x6^2x7x8+9x3x9^3

I would like to ask Sage to give coefficient for the polynomial with variables x1..x9 and the result should be something like this: coefficient,(exponent of x1..x9); 1,(1,0,0,0,0,0,0,0,0); 2,(1,0,0,0,2,0,0,0,0); 3,(2,0,0,1,0,0,0,0,0); -5,(0,1,0,0,2,0,0,0,0); 1/2,(0,1,1,0,0,0,0,0,0); 1,(0,0,0,0,0,2,1,1); 9,(0,0,1,0,0,0,0,0,3)

How can I achieve this?

2018-10-06 04:14:11 -0500 marked best answer Should I always use .expand() before using .coefficient()?

I used the following code in SageMath (Windows binary, version 8.1):

q,x,t,bb_1_1_0,bb_1_1_1,aa_1_1_1,cc_1,eps=var('q x t bb_1_1_0 bb_1_1_1 aa_1_1_1 cc_1 eps')

const1=-(bb_1_1_1*eps*cos(t)*cos(x) + bb_1_1_0*eps*cos(x) + q)*aa_1_1_1*eps*cos(x)*sin(t) - bb_1_1_1*cc_1*eps*cos(x)*sin(t)

Obtaining the coefficients of polynomial with respect to eps: const1.coefficient(eps,1) gives

-(bb_1_1_1*cos(t)*cos(x) + bb_1_1_0*cos(x))*aa_1_1_1*cos(x)*sin(t) - bb_1_1_1*cc_1*cos(x)*sin(t)

but using const1.expand().coefficient(eps,1) gives

-bb_1_1_1*cc_1*cos(x)*sin(t) - aa_1_1_1*q*cos(x)*sin(t)

Clearly the second one is correct. Does this mean that I should always use .expand() before using .coefficient()? In another instance I was forced to use simplify_full() before using coefficient().

2018-10-06 04:06:05 -0500 commented answer Print symbolic variables like a_1 in A[1] style

Yes, I noticed your answer edit. Thanks alot. I was trying to substitute but I had to use a new name, but your approach eliminates the need for declaring new names. Thank you. Also thanks about the sympy advice for automatic C code conversion. Are there options for MATLAB and BASIC languages too?

2018-10-06 03:55:26 -0500 received badge  Scholar (source)
2018-10-06 03:55:08 -0500 commented answer Print symbolic variables like a_1 in A[1] style

Thank you very much! Yes your solution actually works! I will make the substitution at the very end before interfacing to C. Incidentally, since you have been so helpful, would it be possible to give me a hint about what I can do about the conversion of a^b to pow(a,b) problem? Should I iterate over expressions with operands() and factor_list() to identify powers and print out the power formulas in a new format?

2018-10-06 03:43:00 -0500 commented answer Print symbolic variables like a_1 in A[1] style

Thank you for the answer. However, the solution you said is causing problems for me. When I try to apply the trig_reduce() command to something such as (B[2]*cos(x)*cos(2*x)).trig_reduce(), it returns an error unable to make sense of Maxima expression.

2018-10-06 03:37:35 -0500 received badge  Supporter (source)
2018-10-06 03:25:13 -0500 received badge  Student (source)
2018-10-05 22:30:25 -0500 asked a question Print symbolic variables like a_1 in A[1] style

Because of Sagemath's problem with symbolic arrays, I have defined my vectors like this:

A =[0 for j in range(4)]
for j in range(4):
    A[j]=var('a_'+str(j))

I have some symbolic variables stored in another variable. Lets say I have a subroutine that works with A[i] and in the end, L becomes something such as: L=a_1+a_2*a_3. When I print L, I want to have it printed in the original vector format. So when I type L or print(L) in a cell and press enter, my desired output is A[1]+A[2]*A[3] and not a_1+a_2*a_3. I want this type of output because I am transferring SageMath outputs to C where I employ indexed arrays. How can I achieve this?

2018-09-17 04:34:50 -0500 commented answer Function of symbolic array elements

Thank you. Your solution works! With this new method of writing if I have L in either variable type format L=[a_1*b_1, a_0, b_2] or in list indexed format L=[A[1]*B[1],A[0],B[2]] I still get a working function when I type temp(A,B) and there is no need to write out the variables inside each of the lists as the function argument. Of course for the former case, the variables a_0, a_1, a_2 should have be defined somewhere via a_0=var('a_0') or using the non-compact list definition that I used previously. You are a master! Thanks! Just a question, can you explain to me how dict and zip work?

2018-09-16 11:06:06 -0500 edited question Function of symbolic array elements

I want to create a function like this:

def f(A,B):
    return [A[1]*B[1],A[0],B[2]]

However, the thing that the function is supposed to return is calculated in an earlier part of the program. Since Sagemath has a problem with symbolic indices, I have previously defined A and B as follows:

Nf=5
B =[0 for j in range(Nf+1)]
A =[0 for j in range(Nf+1)]
for j in range(Nf+1):
    A[j]=var('a_'+str(j))
    B[j]=var('b_'+str(j))

The thing that the function should return is calculated in another part of the program. Suppose that it is something like this (evaluated from a separate subroutine):

temp=[a_1*b_1, a_0, b_2]

So my question is: how do I turn this temp into a function similar to f, and the second question: the matrix element references are replaced by variable names. Even if I define a function for temp as it is, I will have to define a huge number of input variables f(a_1, b_1, a_0, b_2, ...) for the function whereas if I could just replace the variables a_1 with A[1], etc., this would have enabled me to use f(A,B) making the function definition so much more compact. How can I achieve this?