### C++ dolfin::norm only on part of function

146

views

0

Hello,

I've got a function from which I need to calculate a L1-norm. I tried it like this:

I've got a function from which I need to calculate a L1-norm. I tried it like this:

*dolfin::norm(func.vector(), std::string("l1));*

Unfortunately the function contains at least one element which is "NaN". Is there a way to create a new function with the same content despite the "NaNs" or to calculate the norm only on a part of the function?

Thanks in advance,

Justus

Justus

Community: FEniCS Project

### 1 Answer

4

You can calculate the norm manually by using ``func.vector().array()`` to access numpy array. You can then replace any nans of the array with numpy too:

https://docs.scipy.org/doc/numpy/reference/generated/numpy.nan_to_num.html

https://docs.scipy.org/doc/numpy/reference/generated/numpy.nan_to_num.html

is there a similar way to do this in C++?

written
4 months ago by
Justus Schock

1

You have access to the dolfin::Function.vector()->get_local(), so from there I don't know if there is something in std to replace Nan's, perhaps just a for loop?

written
4 months ago by
pf4d

```
std::vector<double> values;
diff_func.vector()->get_local(values);
for(unsigned int i = 0; i < values.size(); i++){
if (values[i] != values[i]){
values[i] = 0.0;
}
}
```

did the trick. I only did not knew how to access the vectors content. Thanks a lot.

written
4 months ago by
Justus Schock

Happy to be of service!

written
4 months ago by
pf4d

Please login to add an answer/comment or follow this question.