Ask Your Question
1

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

asked 9 years ago

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

Preview: (hide)

Comments

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 ( 9 years ago )

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 ( 9 years ago )

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

slelievre gravatar imageslelievre ( 9 years ago )

1 Answer

Sort by » oldest newest most voted
1

answered 9 years ago

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()

Preview: (hide)
link

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

Stats

Asked: 9 years ago

Seen: 1,298 times

Last updated: Jun 30 '16