triangleWave function
dynamic
triangleWave(
- dynamic x
Triangle wave function
The triangle wave function generates a periodic waveform that oscillates in a triangle shape.
Example:
print(triangleWave(2.7)); // Output: -0.8
print(triangleWave(3.2)); // Output: 0.8
The output for 2.7 is -0.8 and for 0.8 is -0.6 because the triangle wave function oscillates in a triangle shape.
Example 2:
print(triangleWave(Matrix.identity(2))); // Output: [[1.0, 0.0], [0.0, 1.0]]
Implementation
dynamic triangleWave(dynamic x) {
if (x is num) {
x %= 1;
if (x < 0.25) {
return 4 * x;
} else if (x < 0.75) {
return -4 * x + 2;
} else {
return 4 * x - 4;
}
} else if (x is Complex) {
return triangleWave(x.real);
} else if (x is Matrix) {
return MatrixFunctions(x).triangleWave();
} else {
throw ArgumentError('Input should be either num or Complex');
}
}