Ask Your Question

Convert all complex numbers in array/matrix to value 0 or remove these numbers

asked 2016-06-18 21:39:34 +0200

this post is marked as community wiki

This post is a wiki. Anyone with karma >750 is welcome to improve it.

Hi all

I'm new to sagemath, approximately 2 week-old. How do you convert all complex numbers in a nested array to value 0 or NaN? Or to remove these numbers from the array/matrix(which is eventually what I want to achieve in the end)?

Thanks for your help!

Rgds Sam

edit retag flag offensive close merge delete


Could you please provide an example of an array, and the expected result, it is not clear to me. Are all entries complex numbers ?

tmonteil gravatar imagetmonteil ( 2016-06-19 17:18:20 +0200 )edit

1)I did a "solve" on 2 nonlinear equations with variables var1 and var2. z = solve 2)I made 2 empty lists : var1_y = [], var2_y = [] then i appended the var 1 and var 2 solutions separately for solution in z: var1_y.append(var1.subs(solution)) var2_y.append(var2.subs(solution)) 3) then I combine them into matrix. How do I delete the rows with complex no? Thx! var1_y2 = matrix(var1_y).transpose() var2_y2 = matrix(var2)y).transpose() A = var1_y2.augment(var2_y2) [ 2 0] [ -1/10 0] [0.5186911103074608 - 0.8859791354693165I 0.1849665371636101 +0.1317620873508901I] [ 2.887006775607812 -0.1147163491731212

sam_kjm gravatar imagesam_kjm ( 2016-06-20 03:57:42 +0200 )edit

Try editing your question; comments don't leave you enough room to provide enough information.

slelievre gravatar imageslelievre ( 2016-06-20 10:07:16 +0200 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2016-06-30 17:57:42 +0200

sam_kjm gravatar image

I did the following to solve this: 1) after solution and appending to lists, I stack lists into matrix 2) convert type from object to 'float64' 3) start a conditional to convert all terms with imag(x) != 0 into nan 4) do a mask to eliminate all rows(axis=1) that contains any term that gives true to isnan()

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2016-06-18 21:39:34 +0200

Seen: 864 times

Last updated: Jun 30 '16