Skip to content
LIBRARY
Math.Pythagoras.md

Math.Pythagoras

Determine the hypotenuse or leg of a right triangle.

This block determines the hypotenuse y = sqrt(u1^2 + u2^2) if the boolean parameter u1IsHypotenuse = false. In this case the two inputs u1 and u2 are interpreted as the legs of a right triangle and the boolean output valid is always equal to true.

If u1IsHypotenuse = true, input u1 is interpreted as hypotenuse and u2 is one of the two legs of a right triangle. Then, the other of the two legs of the right triangle is the output, determined by y = sqrt(u1^2 - u2^2), if u1^2 - u2^2 >= 0; in this case the boolean output valid is equal to true. In case of u1^2 - u2^2 < 0, the output y = 0 and valid is set to false.

This component extends from BlockComponents.Interfaces.SI2SO

Usage

BlockComponents.Math.Pythagoras(u1IsHypotenuse=false)

Parameters:

NameDescriptionUnitsDefault value
u1IsHypotenuseIf true, u1 is the hypotenuse and y is one legfalse

Connectors

  • u1 - This connector represents a real signal as an input to a component (RealInput)

  • u2 - This connector represents a real signal as an input to a component (RealInput)

  • y - This connector represents a real signal as an output from a component (RealOutput)

  • valid - This connector represents a boolean signal as an output from a component (BooleanOutput)

Variables

NameDescriptionUnits
y2Square of y

Behavior

Source

dyad
"""
Determine the hypotenuse or leg of a right triangle.

This block determines the hypotenuse `y = sqrt(u1^2 + u2^2)` if the boolean parameter
`u1IsHypotenuse = false`. In this case the two inputs `u1` and `u2` are interpreted as the
legs of a right triangle and the boolean output `valid` is always equal to `true`.

If `u1IsHypotenuse = true`, input `u1` is interpreted as hypotenuse and `u2` is one of the
two legs of a right triangle. Then, the other of the two legs of the right triangle is the
output, determined by `y = sqrt(u1^2 - u2^2)`, if `u1^2 - u2^2 >= 0`; in this case the
boolean output `valid` is equal to `true`. In case of `u1^2 - u2^2 < 0`, the output `y = 0`
and `valid` is set to `false`.
"""
component Pythagoras
  extends BlockComponents.Interfaces.SI2SO
  "True if y is a valid result"
  valid = BooleanOutput() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 1000, "y1": 830, "x2": 1100, "y2": 930, "rot": 0}
      },
      "tags": []
    }
  }
  "If true, u1 is the hypotenuse and y is one leg"
  parameter u1IsHypotenuse::Boolean = false
  "Square of y"
  variable y2::Real
relations
  y2 = ifelse(u1IsHypotenuse, u1 ^ 2 - u2 ^ 2, u1 ^ 2 + u2 ^ 2)
  valid = ifelse(u1IsHypotenuse, y2 >= 0, true)
  y = ifelse(u1IsHypotenuse, ifelse(y2 >= 0, sqrt(y2), 0.0), sqrt(y2))
metadata {
  "Dyad": {
    "labels": [{"label": "$(instance)", "x": 500, "y": 1100, "rot": 0}],
    "icons": {"default": "dyad://BlockComponents/Pythagoras.svg"}
  }
}
end
Flattened Source
dyad
"""
Determine the hypotenuse or leg of a right triangle.

This block determines the hypotenuse `y = sqrt(u1^2 + u2^2)` if the boolean parameter
`u1IsHypotenuse = false`. In this case the two inputs `u1` and `u2` are interpreted as the
legs of a right triangle and the boolean output `valid` is always equal to `true`.

If `u1IsHypotenuse = true`, input `u1` is interpreted as hypotenuse and `u2` is one of the
two legs of a right triangle. Then, the other of the two legs of the right triangle is the
output, determined by `y = sqrt(u1^2 - u2^2)`, if `u1^2 - u2^2 >= 0`; in this case the
boolean output `valid` is equal to `true`. In case of `u1^2 - u2^2 < 0`, the output `y = 0`
and `valid` is set to `false`.
"""
component Pythagoras
  "First real-valued input signal"
  u1 = RealInput() {
    "Dyad": {
      "placement": {
        "icon": {"iconName": "default", "x1": -100, "y1": 150, "x2": 0, "y2": 250, "rot": 0},
        "diagram": {"iconName": "default", "x1": -100, "y1": 150, "x2": 0, "y2": 250, "rot": 0}
      },
      "tags": []
    }
  }
  "Second real-valued input signal"
  u2 = RealInput() {
    "Dyad": {
      "placement": {
        "icon": {"iconName": "default", "x1": -100, "y1": 750, "x2": 0, "y2": 850, "rot": 0},
        "diagram": {"iconName": "default", "x1": -100, "y1": 750, "x2": 0, "y2": 850, "rot": 0}
      }
    }
  }
  "Real-valued output signal"
  y = RealOutput() {
    "Dyad": {
      "placement": {
        "icon": {"iconName": "default", "x1": 1000, "y1": 450, "x2": 1100, "y2": 550, "rot": 0},
        "diagram": {"iconName": "default", "x1": 1000, "y1": 450, "x2": 1100, "y2": 550, "rot": 0}
      }
    }
  }
  "True if y is a valid result"
  valid = BooleanOutput() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 1000, "y1": 830, "x2": 1100, "y2": 930, "rot": 0}
      },
      "tags": []
    }
  }
  "If true, u1 is the hypotenuse and y is one leg"
  parameter u1IsHypotenuse::Boolean = false
  "Square of y"
  variable y2::Real
relations
  y2 = ifelse(u1IsHypotenuse, u1 ^ 2 - u2 ^ 2, u1 ^ 2 + u2 ^ 2)
  valid = ifelse(u1IsHypotenuse, y2 >= 0, true)
  y = ifelse(u1IsHypotenuse, ifelse(y2 >= 0, sqrt(y2), 0.0), sqrt(y2))
metadata {
  "Dyad": {
    "labels": [{"label": "$(instance)", "x": 500, "y": 1100, "rot": 0}],
    "icons": {"default": "dyad://BlockComponents/Pythagoras.svg"}
  }
}
end


Test Cases

No test cases defined.