Repository: magpylib/magpylib

Issue:
H-field inside for Cylinder and Cylinder Segment does not match
### Magpylib version

4.x (Latest)

### What happened?

When computing the H-field inside a cylinder, the values do not match the values obtained with an equivalent cylinder segment.


@OrtnerMichael Shall we wait for the v5 release or make a hotfix for v4 already now?


### Code example

```Python
import magpylib as magpy
import numpy as np

field = "H"
obs = np.array([(0.1, 0.2, 0.3)])
pols = np.array([(0, 0, 1)] * len(obs))
d,h = 1,1
dims = np.array([(d, h)]*len(obs))
dims_cs = np.array([(0, d/2, h, 0, 360)]*len(obs))

Bc = magpy.core.magnet_cylinder_field(
    field=field,
    observers=obs,
    dimension=dims,
    magnetization=pols,
)
Bcs = magpy.core.magnet_cylinder_segment_field(
    field=field,
    observers=obs,
    dimension=dims_cs,
    magnetization=pols,
)
print(Bc, Bcs)
# ->output :
#(array([[0.02672707, 0.05345415, 0.50743767]]),
# array([[ 0.02672707,  0.05345415, -0.28833704]]))
```


### Additional context

`Cylinder` field function axial vs transversal mask. seems to be the issue
```python
      if any(mask_ax):  # ax computes B-field
        Bz[mask_tv * mask_inside] -= magz[mask_tv * mask_inside]
    return np.concatenate(((Bx,), (By,), (Bz,)), axis=0).T * 10 / 4 / np.pi
    ```
    ([permalink](https://github.com/magpylib/magpylib/blob/e35b724a8154903d2c6710edb1d254105d884f88/magpylib/_src/fields/field_BH_cylinder.py#L388C5-L389C65))

Discussion:
Alexboiboi: @OrtnerMichael Is this ok?

Patch:
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index 31111ad4..d740eb9e 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -25,7 +25,7 @@ repos:
 
 
   - repo: https://github.com/psf/black
-    rev: "23.12.0"
+    rev: "23.12.1"
     hooks:
       - id: black
 
diff --git a/magpylib/_src/fields/field_BH_cylinder.py b/magpylib/_src/fields/field_BH_cylinder.py
index ec0f49e1..60245a8f 100644
--- a/magpylib/_src/fields/field_BH_cylinder.py
+++ b/magpylib/_src/fields/field_BH_cylinder.py
@@ -386,7 +386,7 @@ def magnet_cylinder_field(
         return np.concatenate(((Bx,), (By,), (Bz,)), axis=0).T
 
     if any(mask_ax):  # ax computes B-field
-        Bz[mask_tv * mask_inside] -= magz[mask_tv * mask_inside]
+        Bz[mask_ax * mask_inside] -= magz[mask_ax * mask_inside]
     return np.concatenate(((Bx,), (By,), (Bz,)), axis=0).T * 10 / 4 / np.pi
 
 


Test patch:
diff --git a/tests/test_field_cylinder.py b/tests/test_field_cylinder.py
index 1934015c..7848b867 100644
--- a/tests/test_field_cylinder.py
+++ b/tests/test_field_cylinder.py
@@ -461,3 +461,26 @@ def test_cylinder_diametral_small_r():
     ddB = abs(ddB - np.mean(ddB, axis=0))
 
     assert np.all(ddB < 0.001)
+
+
+def test_cyl_vs_cylseg_axial_H_inside_mask():
+    """see https://github.com/magpylib/magpylib/issues/703"""
+    field = "H"
+    obs = np.array([(0.1, 0.2, 0.3)])
+    pols = np.array([(0, 0, 1)])
+    dims = np.array([(1, 1)])
+    dims_cs = np.array([(0, 0.5, 1, 0, 360)])
+
+    Bc = magpy.core.magnet_cylinder_field(
+        field=field,
+        observers=obs,
+        dimension=dims,
+        magnetization=pols,
+    )
+    Bcs = magpy.core.magnet_cylinder_segment_field(
+        field=field,
+        observers=obs,
+        dimension=dims_cs,
+        magnetization=pols,
+    )
+    np.testing.assert_allclose(Bc, Bcs)