2019-04-15 10:27:45 8 Comments

In SymPy, there is a useful feature to create a dictionary of coefficients for a given expression.

However, I'm encountering an irritating bug (/feature?) whereby square roots are considered to be part of the variable, and not part of the coefficient value.

Minimum example:

```
from sympy import sqrt, symbols
k, t = symbols('k, t')
d = 1.5*t + 2.5*sqrt(2)*k
d.as_coefficients_dict()
```

This returns:

```
{𝑡:1.5, sqrt(2)*𝑘:2.5}
```

When instead, the sqrt(2) should be considered as part of the coefficient. Thus, I expected to see the result:

```
{𝑡:1.5, 𝑘:2.5*sqrt(2)}
```

NB, I'm using the latest SymPy version 1.4

Is this a bug? Or an alternative way to use the function to get the expected value please?

EDIT: I amended the question to note that I am using the Sympy sqrt function. I also tried using NumPy's np.sqrt, which evaluates correctly, but gives the full numerical value rather than a nice neat sqrt(2) for the value of the k coefficient.

### Related Questions

#### Sponsored Content

#### 32 Answered Questions

### [SOLVED] "Least Astonishment" and the Mutable Default Argument

**2009-07-15 18:00:37****Stefano Borini****130059**View**2283**Score**32**Answer- Tags: python language-design default-parameters least-astonishment

#### 10 Answered Questions

#### 1 Answered Questions

### [SOLVED] Sympy does not simplify an expression with a symbol divided by the square root of its square

**2018-04-21 13:55:14****Sergey****118**View**1**Score**1**Answer- Tags: python sympy symbolic-math

#### 1 Answered Questions

#### 1 Answered Questions

#### 2 Answered Questions

### [SOLVED] SymPy and square roots of complex numbers

**2016-12-25 21:02:58****Frunobulax****548**View**5**Score**2**Answer- Tags: python sympy symbolic-math

#### 1 Answered Questions

### How to simplify sqrt expressions in sympy

**2016-04-22 16:13:19****kwinkunks****826**View**6**Score**1**Answer- Tags: python sympy symbolic-math

#### 1 Answered Questions

### [SOLVED] Factor sympy expression to matrix coefficients?

**2015-05-07 22:19:30****theNamesCross****772**View**4**Score**1**Answer- Tags: python matrix sympy symbolic-math

## 1 comments

## @Jonas Adler 2019-04-15 10:59:31

The documentation explicitly states:

So to start with, this is the expected behavior. To see this, you can note that the sqrt is

notthe problem, rather it is the fact that the coefficient is irrational. If we take a rational coefficient, we get your expected behavior:One way to solve your problem is to explicitly inquire about each coefficient with the given variables:

## @SLhark 2019-04-15 11:37:07

Thanks for clarifying the expected behaviour. The workaround you posted works nicely thanks, although with the only limitation that I now need to explicitly specify the variables. This isn't a big issue though.