---
url: /dyad/stable/stdlib/BlockComponents/tests/Math.Tests.RectangularToPolar.md
description: >-
  Dyad Math.Tests.RectangularToPolar docs.  Converts constant and time-varying
  rectangular coordinates to polar.
---

# `Math.Tests.RectangularToPolar` {#Math.Tests.RectangularToPolar}

Converts constant and time-varying rectangular coordinates to polar.

Connects constant sources with u_re=3 and u_im=4 to a RectangularToPolar block and verifies that y_abs = sqrt(9+16) = 5 and y_arg = atan2(4,3) ≈ 0.9273. A second RectangularToPolar block holds the real part at 3 while a sine wave sweeps the imaginary part, so the magnitude and angle vary over time.

## Usage {#Usage}

`BlockComponents.Math.Tests.RectangularToPolar()`

## Behavior {#Behavior}

$$ \begin{align}
0 &= \mathrm{connect}\left( c\_{re\_{+}y}, r2p\_{+}u\_{re} \right) \\
0 &= \mathrm{connect}\left( c\_{im\_{+}y}, r2p\_{+}u\_{im} \right) \\
0 &= \mathrm{connect}\left( c\_{re\_{+}y}, r2p\_{2\_{+}u_re} \right) \\
0 &= \mathrm{connect}\left( im\_{sine\_{+}y}, r2p\_{2\_{+}u_im} \right) \\
\mathtt{c_re.y}\left( t \right) &= \mathtt{c_re.k} \\
\mathtt{c_im.y}\left( t \right) &= \mathtt{c_im.k} \\
\mathtt{r2p.y_abs}\left( t \right) &= \sqrt{\left( \mathtt{r2p.u_im}\left( t \right) \right)^{2} + \left( \mathtt{r2p.u_re}\left( t \right) \right)^{2}} \\
\mathtt{r2p.y_arg}\left( t \right) &= \arctan\left( \mathtt{r2p.u_im}\left( t \right), \mathtt{r2p.u_re}\left( t \right) \right) \\
\mathtt{im_sine.y}\left( t \right) &= ifelse\left( \mathtt{im_sine.continuous}, ifelse\left( t \geq \mathtt{im_sine.start_time}, \sin\left( \mathtt{im_sine.phase} + 6.2832 ~ \mathtt{im_sine.frequency} ~ \left(  - \mathtt{im_sine.shift_time} + t \right) \right), \sin\left( \mathtt{im_sine.phase} + 6.2832 ~ \mathtt{im_sine.frequency} ~ \left(  - \mathtt{im_sine.shift_time} + \mathtt{im_sine.start_time} \right) \right) \right) ~ \mathtt{im_sine.amplitude}, ifelse\left( t \geq \mathtt{im_sine.start_time}, \mathtt{im_sine.amplitude} ~ \sin\left( \mathtt{im_sine.phase} + 6.2832 ~ \mathtt{im_sine.frequency} ~ \left(  - \mathtt{im_sine.shift_time} + t \right) \right), 0 \right) \right) + \mathtt{im_sine.offset} \\
\mathtt{r2p_2.y_abs}\left( t \right) &= \sqrt{\left( \mathtt{r2p_2.u_im}\left( t \right) \right)^{2} + \left( \mathtt{r2p_2.u_re}\left( t \right) \right)^{2}} \\
\mathtt{r2p_2.y_arg}\left( t \right) &= \arctan\left( \mathtt{r2p_2.u_im}\left( t \right), \mathtt{r2p_2.u_re}\left( t \right) \right)
\end{align}
$$

## Source {#Source}

```dyad
"""
Converts constant and time-varying rectangular coordinates to polar.

Connects constant sources with u_re=3 and u_im=4 to a RectangularToPolar
block and verifies that y_abs = sqrt(9+16) = 5 and
y_arg = atan2(4,3) ≈ 0.9273. A second RectangularToPolar block holds the real
part at 3 while a sine wave sweeps the imaginary part, so the magnitude and
angle vary over time.
"""
test component RectangularToPolar
  "Constant source providing the real part"
  c_re = BlockComponents.Sources.Constant(k = 3) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 20, "y1": 20, "x2": 120, "y2": 120, "rot": 0}
      },
      "tags": []
    }
  }
  "Constant source providing the imaginary part"
  c_im = BlockComponents.Sources.Constant(k = 4) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 20, "y1": 140, "x2": 120, "y2": 240, "rot": 0}
      },
      "tags": []
    }
  }
  "RectangularToPolar block under test"
  r2p = BlockComponents.Math.RectangularToPolar() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 180, "y1": 50, "x2": 280, "y2": 150, "rot": 0}
      },
      "tags": []
    }
  }
  "Sine source sweeping the imaginary part of the second block"
  im_sine = BlockComponents.Sources.Sine(amplitude = 4, frequency = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 20, "y1": 260, "x2": 120, "y2": 360, "rot": 0}
      },
      "tags": []
    }
  }
  "Second RectangularToPolar block with a swept imaginary part"
  r2p_2 = BlockComponents.Math.RectangularToPolar() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 170, "y1": 236, "x2": 270, "y2": 336, "rot": 0}
      },
      "tags": []
    }
  }
relations
  connect(c_re.y, r2p.u_re) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 145, "y": 70}, {"x": 145, "y": 71}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(c_im.y, r2p.u_im) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 140, "y": 190}, {"x": 140, "y": 124}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(c_re.y, r2p_2.u_re) {
    "Dyad": {
      "edges": [{"S": 1, "M": [], "E": -1}, {"S": -1, "M": [{"x": 150, "y": 257}], "E": 2}],
      "junctions": [{"x": 150, "y": 70}],
      "renderStyle": "standard"
    }
  }
  connect(im_sine.y, r2p_2.u_im) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
metadata {
  "Dyad": {
    "icons": {"default": "dyad://BlockComponents/Example.svg"},
    "tests": {
      "case1": {
        "stop": 1,
        "expect": {
          "signals": ["r2p.y_abs", "r2p.y_arg", "r2p_2.y_abs", "r2p_2.y_arg", "im_sine.y"]
        }
      }
    }
  }
}
end
```

```dyad
"""
Converts constant and time-varying rectangular coordinates to polar.

Connects constant sources with u_re=3 and u_im=4 to a RectangularToPolar
block and verifies that y_abs = sqrt(9+16) = 5 and
y_arg = atan2(4,3) ≈ 0.9273. A second RectangularToPolar block holds the real
part at 3 while a sine wave sweeps the imaginary part, so the magnitude and
angle vary over time.
"""
test component RectangularToPolar
  "Constant source providing the real part"
  c_re = BlockComponents.Sources.Constant(k = 3) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 20, "y1": 20, "x2": 120, "y2": 120, "rot": 0}
      },
      "tags": []
    }
  }
  "Constant source providing the imaginary part"
  c_im = BlockComponents.Sources.Constant(k = 4) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 20, "y1": 140, "x2": 120, "y2": 240, "rot": 0}
      },
      "tags": []
    }
  }
  "RectangularToPolar block under test"
  r2p = BlockComponents.Math.RectangularToPolar() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 180, "y1": 50, "x2": 280, "y2": 150, "rot": 0}
      },
      "tags": []
    }
  }
  "Sine source sweeping the imaginary part of the second block"
  im_sine = BlockComponents.Sources.Sine(amplitude = 4, frequency = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 20, "y1": 260, "x2": 120, "y2": 360, "rot": 0}
      },
      "tags": []
    }
  }
  "Second RectangularToPolar block with a swept imaginary part"
  r2p_2 = BlockComponents.Math.RectangularToPolar() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 170, "y1": 236, "x2": 270, "y2": 336, "rot": 0}
      },
      "tags": []
    }
  }
relations
  connect(c_re.y, r2p.u_re) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 145, "y": 70}, {"x": 145, "y": 71}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(c_im.y, r2p.u_im) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 140, "y": 190}, {"x": 140, "y": 124}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(c_re.y, r2p_2.u_re) {
    "Dyad": {
      "edges": [{"S": 1, "M": [], "E": -1}, {"S": -1, "M": [{"x": 150, "y": 257}], "E": 2}],
      "junctions": [{"x": 150, "y": 70}],
      "renderStyle": "standard"
    }
  }
  connect(im_sine.y, r2p_2.u_im) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
metadata {
  "Dyad": {
    "icons": {"default": "dyad://BlockComponents/Example.svg"},
    "tests": {
      "case1": {
        "stop": 1,
        "expect": {
          "signals": ["r2p.y_abs", "r2p.y_arg", "r2p_2.y_abs", "r2p_2.y_arg", "im_sine.y"]
        }
      }
    }
  }
}
end
```

## Test Cases {#Test-Cases}

### Test Case `case1` {#Test-Case-case1}

```julia
plt
```

![](mjvhshk.svg)

```julia
plt
```

![](rrgonxe.svg)

```julia
plt
```

![](omuhsng.svg)

```julia
plt
```

![](rrslsyx.svg)

```julia
plt
```

![](fqufusv.svg)

## Related {#Related}

* Examples

* Experiments

* Analyses

* Tests
  * [`Constant`](Math.Tests.RectangularToPolar.md)

  * [`RectangularToPolar`](Math.Tests.RectangularToPolar.md)

  * [`RectangularToPolar`](Math.Tests.RectangularToPolar.md)

  * [`Sine`](Math.Tests.RectangularToPolar.md)
