squareWave function

dynamic squareWave(
  1. dynamic x
)

Square wave function

The square wave function generates a periodic waveform which alternates between two levels.

Example:

print(squareWave(2.7));  // Output: -1.0
print(squareWave(3.2));  // Output: 1.0

The output for 2.7 is 1.0 and for 3.2 is -1.0 because the square wave function oscillates between 1.0 and -1.0.

Example 2:

print(squareWave(Matrix.identity(2))); // Output: [[1.0, 0.0], [0.0, 1.0]]

Implementation

dynamic squareWave(dynamic x) {
  if (x is num) {
    return (x % 2 < 1) ? -1 : 1;
  } else if (x is Complex) {
    return squareWave(x.real);
  } else if (x is Matrix) {
    return MatrixFunctions(x).squareWave();
  } else {
    throw ArgumentError('Input should be either num or Complex');
  }
}