# Using numpy array in the function that calculates residuals in DAEProblem (diffeqpy)

**URL:** <https://discourse.julialang.org/t/using-numpy-array-in-the-function-that-calculates-residuals-in-daeproblem-diffeqpy/112450>\
**Category:** General Usage\
**Tags:** diffeq\
**Created:** [April 3, 2024, 5:21am UTC](https://discourse.julialang.org/t/using-numpy-array-in-the-function-that-calculates-residuals-in-daeproblem-diffeqpy/112450 "2024-04-03T05:21:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![anjali](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/anjali/32/205427_2.png) [@anjali](https://discourse.julialang.org/u/anjali)\
**Post date:** [April 3, 2024, 5:21am UTC](https://discourse.julialang.org/t/using-numpy-array-in-the-function-that-calculates-residuals-in-daeproblem-diffeqpy/112450/1 "2024-04-03T05:21:09Z")

</div>

I am trying to use `diffeqpy` to use DAESolver from my python code.

The following code **works** :

```python
# the residual calculation function
def f(du, u, p, t):
    resid1 = -0.04 * u[0] + 1e4 * u[1] * u[2] - du[0]
    resid2 = +0.04 * u[0] - 3e7 * u[1] ** 2 - 1e4 * u[1] * u[2] - du[1]
    resid3 = u[0] + u[1] + u[2] - 1.0
    res = [resid1, resid2, resid3]
    return res

# initial values
u0 = [1.0, 0.0, 0.0]
du0 = [-0.04, 0.04, 0.0]
tspan = (0.0, 1.0)
differential_vars = [True, True, False]

# crate the DAE problem and solve
prob = de.DAEProblem(f, du0, u0, tspan, differential_vars=differential_vars)
sol = de.solve(prob, de.IDA(), abstol=1e-8, reltol=1e-8)

```

The following **fails** :

```python
# the residual calculation function
def f(du, u, p, t):
    resid1 = -0.04 * u[0] + 1e4 * u[1] * u[2] - du[0]
    resid2 = +0.04 * u[0] - 3e7 * u[1] ** 2 - 1e4 * u[1] * u[2] - du[1]
    resid3 = u[0] + u[1] + u[2] - 1.0
    res = np.array([resid1, resid2, resid3]) // <---- changed
    return res

# initial values
u0 = np.array([1.0, 0.0, 0.0]) // <----- changed
du0 = np.array([-0.04, 0.04, 0.0]) // <----- changed
tspan = (0.0, 1.0)
differential_vars = [True, True, False]

# crate the DAE problem and solve
prob = de.DAEProblem(f, du0, u0, tspan, differential_vars=differential_vars)
sol = de.solve(prob, de.IDA(), abstol=1e-8, reltol=1e-8)

```

The following is the error: (I can share the complete error output if it helps):

```julia
juliacall.JuliaError: MethodError: Cannot `convert` an object of type Vector{Float64} to an object of type PyArray{Float64, 1, true, true, Float64}

```

The part calling the solver is a part of a larger system wherein we have generated the matrices for the system and wish to solve it. The residual function here (`f`) therefore needs to operate on numpy arrays.

I am new to Julia. Is this something that is possible at all? Alternatively, is it possible to modify the above in any way to make it work, like converting those numpy matrices to julia ones and doing the residual calculation on the julia side?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [April 4, 2024, 8:32am UTC](https://discourse.julialang.org/t/using-numpy-array-in-the-function-that-calculates-residuals-in-daeproblem-diffeqpy/112450/2 "2024-04-04T08:32:36Z")

</div>

> [@anjali](#):
>
> The following is the error: (I can share the complete error output if it helps)

Share the complete error, that would be helpful.
