# Programmable filter - eigenvalues/lambda2

**URL:** https://discourse.paraview.org/t/programmable-filter-eigenvalues-lambda2/12188
**Category:** ParaView Support
**Created:** [May 30, 2023, 3:14pm UTC](https://discourse.paraview.org/t/programmable-filter-eigenvalues-lambda2/12188 "2023-05-30T15:14:36Z")
**Posts on this page:** 1
**Showing post:** 9

<div class="post-metadata">

### Author: ![bastian](https://discourse.paraview.org/user_avatar/discourse.paraview.org/bastian/32/7513_2.png) [@bastian](https://discourse.paraview.org/u/bastian)
#### Post date: [August 18, 2023, 4:54pm UTC](https://discourse.paraview.org/t/programmable-filter-eigenvalues-lambda2/12188/9 "2023-08-18T16:54:50Z")

</div>

> [@flotus1](#):
>
> My script looks like this:
> 
> ```auto
> import numpy as np
> from vtk.numpy_interface import algorithms as algs
> 
> input=inputs[0]
> Velocity_Vector = input.PointData['velocity']
> STRAIN = strain(Velocity_Vector)
> V_gradient = gradient(Velocity_Vector)
> AAA=STRAIN**2+(V_gradient - STRAIN)**2
> print(shape(AAA))
> Lambda_2 = np.linalg.eigvals(AAA[:])
> Lambda_2 = np.real(Lambda_2)
> output.PointData.append(Lambda_2, 'Lambda_2')
> 
> ```

I think that you have a few errors in your code:

1. `STRAIN**2` is probably an elementwise square. What you actually want is the matrix product that matches the signature _(n,k),(k,m)→(n,m)_, which matmul should provide
2. `np.linalg.eigvals` does not order the eigenvalues, but you want lambda\_2 to correspond to the second largest eigenvalue.

I wrote a filter that should give you the proper result, but I haven’t validated it:

```python
import numpy as np
from vtk.numpy_interface import algorithms as algs

# Compute the lambda_2 Vortex criterion, see pp.76-77 in
# J. Jeong and F. Hussain, “On the identification of a vortex,” 
# Journal of Fluid Mechanics, vol. 285, pp. 69–94, 1995, doi: 10.1017/S0022112095000462.

vvector = inputs[0].PointData['velocity']

vstrain = strain(vvector)
vskew = gradient(vvector) - vstrain

aaa = matmul(vstrain, vstrain) + matmul(vskew, vskew)

# since aaa is symmetric, it only has real eigenvalues
lambdas = eigenvalue(aaa)
lambdas = real(lambdas)
lambda2 = sort(lambdas)[:,1]

output.PointData.append(lambda2, 'lambda2')

```

---

_[View the full topic](https://discourse.paraview.org/t/programmable-filter-eigenvalues-lambda2/12188)._
