Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added DoubleAsStringWithPrecision function - Linear Algebra #1891

Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions katas/content/linear_algebra/Common.qs
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
namespace Kata.Verification {
open Microsoft.Quantum.Math;
open Microsoft.Quantum.Convert;

function ComplexAsString(x : Complex) : String {
let precision = 3;
if x::Imag < 0.0 {
$"{DoubleAsStringWithPrecision(x::Real,precision)} - {DoubleAsStringWithPrecision(AbsD(x::Imag),precision)}i"
} else {
$"{DoubleAsStringWithPrecision(x::Real,precision)} + {DoubleAsStringWithPrecision(x::Imag,precision)}i"
}
}

function ArraysEqualD(actual : Double[][], expected : Double[][]) : Bool {
let precision = 3;
if Length(actual) != Length(expected) {
Message("Incorrect");
Message($"Expected number of rows {Length(expected)}, actual {Length(actual)}");
Expand All @@ -14,10 +25,10 @@ namespace Kata.Verification {
return false;
}

for j in 0 .. Length(actual[i]) - 1 {
for j in 0 .. Length(actual[i]) - 1 {
if AbsD(actual[i][j] - expected[i][j]) > 1e-9 {
Message("Incorrect");
Message($"For element in row {i}, column {j}, expected {expected[i][j]}, actual {actual[i][j]}");
Message($"For element in row {i}, column {j}, expected {DoubleAsStringWithPrecision(expected[i][j],precision)}, actual {DoubleAsStringWithPrecision(actual[i][j],precision)}");
return false;
}
}
Expand All @@ -27,15 +38,8 @@ namespace Kata.Verification {
return true;
}

function ComplexAsString(x : Complex) : String {
if x::Imag < 0.0 {
$"{x::Real} - {AbsD(x::Imag)}i"
} else {
$"{x::Real} + {x::Imag}i"
}
}

function ArraysEqualC(actual : Complex[][], expected : Complex[][]) : Bool {
let precision = 3;
if Length(actual) != Length(expected) {
Message("Incorrect");
Message($"Expected number of rows {Length(expected)}, actual {Length(actual)}");
Expand Down