{ "exercise": "complex-numbers", "version": "1.2.0", "comments": [ " The canonical data assumes mathematically correct real ", " numbers. The testsuites should consider rounding errors ", " instead of testing for exact values for any non-integer ", " tests. ", " Complex numbers z are represented as arrays [x, y] so ", " that z = x + i * y. ", " Pi is represented as a string \"pi\", euler's number is ", " represented as \"e\". " ], "cases": [ { "description": "Real part", "cases": [ { "description": "Real part of a purely real number", "property": "real", "input": { "z": [1, 0] }, "expected": 1 }, { "description": "Real part of a purely imaginary number", "property": "real", "input": { "z": [0, 1] }, "expected": 0 }, { "description": "Real part of a number with real and imaginary part", "property": "real", "input": { "z": [1, 2] }, "expected": 1 } ] }, { "description": "Imaginary part", "cases": [ { "description": "Imaginary part of a purely real number", "property": "imaginary", "input": { "z": [1, 0] }, "expected": 0 }, { "description": "Imaginary part of a purely imaginary number", "property": "imaginary", "input": { "z": [0, 1] }, "expected": 1 }, { "description": "Imaginary part of a number with real and imaginary part", "property": "imaginary", "input": { "z": [1, 2] }, "expected": 2 } ] }, { "description": "Imaginary unit", "property": "mul", "input": { "z1": [0, 1], "z2": [0, 1] }, "expected": [-1, 0] }, { "description": "Arithmetic", "cases": [ { "description": "Addition", "cases": [ { "description": "Add purely real numbers", "property": "add", "input": { "z1": [1, 0], "z2": [2, 0] }, "expected": [3, 0] }, { "description": "Add purely imaginary numbers", "property": "add", "input": { "z1": [0, 1], "z2": [0, 2] }, "expected": [0, 3] }, { "description": "Add numbers with real and imaginary part", "property": "add", "input": { "z1": [1, 2], "z2": [3, 4] }, "expected": [4, 6] } ] }, { "description": "Subtraction", "cases": [ { "description": "Subtract purely real numbers", "property": "sub", "input": { "z1": [1, 0], "z2": [2, 0] }, "expected": [-1, 0] }, { "description": "Subtract purely imaginary numbers", "property": "sub", "input": { "z1": [0, 1], "z2": [0, 2] }, "expected": [0, -1] }, { "description": "Subtract numbers with real and imaginary part", "property": "sub", "input": { "z1": [1, 2], "z2": [3, 4] }, "expected": [-2, -2] } ] }, { "description": "Multiplication", "cases": [ { "description": "Multiply purely real numbers", "property": "mul", "input": { "z1": [1, 0], "z2": [2, 0] }, "expected": [2, 0] }, { "description": "Multiply purely imaginary numbers", "property": "mul", "input": { "z1": [0, 1], "z2": [0, 2] }, "expected": [-2, 0] }, { "description": "Multiply numbers with real and imaginary part", "property": "mul", "input": { "z1": [1, 2], "z2": [3, 4] }, "expected": [-5, 10] } ] }, { "description": "Division", "cases": [ { "description": "Divide purely real numbers", "property": "div", "input": { "z1": [1, 0], "z2": [2, 0] }, "expected": [0.5, 0] }, { "description": "Divide purely imaginary numbers", "property": "div", "input": { "z1": [0, 1], "z2": [0, 2] }, "expected": [0.5, 0] }, { "description": "Divide numbers with real and imaginary part", "property": "div", "input": { "z1": [1, 2], "z2": [3, 4] }, "expected": [0.44, 0.08] } ] } ] }, { "description": "Absolute value", "cases": [ { "description": "Absolute value of a positive purely real number", "property": "abs", "input": { "z": [5, 0] }, "expected": 5 }, { "description": "Absolute value of a negative purely real number", "property": "abs", "input": { "z": [-5, 0] }, "expected": 5 }, { "description": "Absolute value of a purely imaginary number with positive imaginary part", "property": "abs", "input": { "z": [0, 5] }, "expected": 5 }, { "description": "Absolute value of a purely imaginary number with negative imaginary part", "property": "abs", "input": { "z": [0, -5] }, "expected": 5 }, { "description": "Absolute value of a number with real and imaginary part", "property": "abs", "input": { "z": [3, 4] }, "expected": 5 } ] }, { "description": "Complex conjugate", "cases": [ { "description": "Conjugate a purely real number", "property": "conjugate", "input": { "z": [5, 0] }, "expected": [5, 0] }, { "description": "Conjugate a purely imaginary number", "property": "conjugate", "input": { "z": [0, 5] }, "expected": [0, -5] }, { "description": "Conjugate a number with real and imaginary part", "property": "conjugate", "input": { "z": [1, 1] }, "expected": [1, -1] } ] }, { "description": "Complex exponential function", "comments": [ " Defining the exponential function can be optional. ", " If the language used does not have sine and cosine ", " functions in the standard library, this will be ", " significantly more difficult than the rest of the exer- ", " cise and should probably not be part of the problem. ", " The recommended implementation uses Euler's formula ", " exp(ix) = cos(x) + i * sin(x). This is not an ideal sol- ", " ution but works for the purpose of teaching complex ", " numbers. " ], "cases": [ { "description": "Euler's identity/formula", "property": "exp", "input": { "z": [0, "pi"] }, "expected": [-1, 0] }, { "description": "Exponential of 0", "property": "exp", "input": { "z": [0, 0] }, "expected": [1, 0] }, { "description": "Exponential of a purely real number", "property": "exp", "input": { "z": [1, 0] }, "expected": ["e", 0] } ] } ] }