-
-
Notifications
You must be signed in to change notification settings - Fork 358
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WIP] Add complex numbers exercise - Part I (#378)
* Add complex numbers exercise - Part I * Update exercise to use ComplexNumber class * Change to class with instance methods * Fix build error after merge * Add exercise to solution * Convert to double * Fix processing doubles * Add generation for Exp function * Add implementation for exponents * Keep math symbols in exercises * Add custom assert for comparing the complex number class * Re-order methods to reflect implementation change
- Loading branch information
1 parent
080952f
commit ddc5c5b
Showing
8 changed files
with
669 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using System; | ||
|
||
public struct ComplexNumber | ||
{ | ||
public ComplexNumber(double real, double imaginary) | ||
{ | ||
} | ||
|
||
public double Real() | ||
{ | ||
throw new NotImplementedException("You need to implement this function."); | ||
} | ||
|
||
public double Imaginary() | ||
{ | ||
throw new NotImplementedException("You need to implement this function."); | ||
} | ||
|
||
public ComplexNumber Mul(ComplexNumber other) | ||
{ | ||
throw new NotImplementedException("You need to implement this function."); | ||
} | ||
|
||
public ComplexNumber Add(ComplexNumber other) | ||
{ | ||
throw new NotImplementedException("You need to implement this function."); | ||
} | ||
|
||
public ComplexNumber Sub(ComplexNumber other) | ||
{ | ||
throw new NotImplementedException("You need to implement this function."); | ||
} | ||
|
||
public ComplexNumber Div(ComplexNumber other) | ||
{ | ||
throw new NotImplementedException("You need to implement this function."); | ||
} | ||
|
||
public double Abs() | ||
{ | ||
throw new NotImplementedException("You need to implement this function."); | ||
} | ||
|
||
public ComplexNumber Conjugate() | ||
{ | ||
throw new NotImplementedException("You need to implement this function."); | ||
} | ||
|
||
public ComplexNumber Exp() | ||
{ | ||
throw new NotImplementedException("You need to implement this function."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>netcoreapp1.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Compile Remove="Example.cs" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" /> | ||
<PackageReference Include="xunit" Version="2.2.0" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,255 @@ | ||
// This file was auto-generated based on version 1.0.0 of the canonical data. | ||
|
||
using Xunit; | ||
using System; | ||
|
||
public class ComplexNumbersTest | ||
{ | ||
[Fact] | ||
public void Real_part_of_a_purely_real_number() | ||
{ | ||
var sut = new ComplexNumber(1, 0); | ||
Assert.Equal(1, sut.Real()); | ||
} | ||
|
||
[Fact(Skip = "Remove to run test")] | ||
public void Real_part_of_a_purely_imaginary_number() | ||
{ | ||
var sut = new ComplexNumber(0, 1); | ||
Assert.Equal(0, sut.Real()); | ||
} | ||
|
||
[Fact(Skip = "Remove to run test")] | ||
public void Real_part_of_a_number_with_real_and_imaginary_part() | ||
{ | ||
var sut = new ComplexNumber(1, 2); | ||
Assert.Equal(1, sut.Real()); | ||
} | ||
|
||
[Fact(Skip = "Remove to run test")] | ||
public void Imaginary_part_of_a_purely_real_number() | ||
{ | ||
var sut = new ComplexNumber(1, 0); | ||
Assert.Equal(0, sut.Imaginary()); | ||
} | ||
|
||
[Fact(Skip = "Remove to run test")] | ||
public void Imaginary_part_of_a_purely_imaginary_number() | ||
{ | ||
var sut = new ComplexNumber(0, 1); | ||
Assert.Equal(1, sut.Imaginary()); | ||
} | ||
|
||
[Fact(Skip = "Remove to run test")] | ||
public void Imaginary_part_of_a_number_with_real_and_imaginary_part() | ||
{ | ||
var sut = new ComplexNumber(1, 2); | ||
Assert.Equal(2, sut.Imaginary()); | ||
} | ||
|
||
[Fact(Skip = "Remove to run test")] | ||
public void Imaginary_unit() | ||
{ | ||
var sut = new ComplexNumber(0, 1); | ||
var expected = new ComplexNumber(-1, 0); | ||
Assert.Equal(expected.Real(), sut.Mul(new ComplexNumber(0, 1)).Real(), 15); | ||
Assert.Equal(expected.Imaginary(), sut.Mul(new ComplexNumber(0, 1)).Imaginary(), 15); | ||
} | ||
|
||
[Fact(Skip = "Remove to run test")] | ||
public void Add_purely_real_numbers() | ||
{ | ||
var sut = new ComplexNumber(1, 0); | ||
var expected = new ComplexNumber(3, 0); | ||
Assert.Equal(expected.Real(), sut.Add(new ComplexNumber(2, 0)).Real(), 15); | ||
Assert.Equal(expected.Imaginary(), sut.Add(new ComplexNumber(2, 0)).Imaginary(), 15); | ||
} | ||
|
||
[Fact(Skip = "Remove to run test")] | ||
public void Add_purely_imaginary_numbers() | ||
{ | ||
var sut = new ComplexNumber(0, 1); | ||
var expected = new ComplexNumber(0, 3); | ||
Assert.Equal(expected.Real(), sut.Add(new ComplexNumber(0, 2)).Real(), 15); | ||
Assert.Equal(expected.Imaginary(), sut.Add(new ComplexNumber(0, 2)).Imaginary(), 15); | ||
} | ||
|
||
[Fact(Skip = "Remove to run test")] | ||
public void Add_numbers_with_real_and_imaginary_part() | ||
{ | ||
var sut = new ComplexNumber(1, 2); | ||
var expected = new ComplexNumber(4, 6); | ||
Assert.Equal(expected.Real(), sut.Add(new ComplexNumber(3, 4)).Real(), 15); | ||
Assert.Equal(expected.Imaginary(), sut.Add(new ComplexNumber(3, 4)).Imaginary(), 15); | ||
} | ||
|
||
[Fact(Skip = "Remove to run test")] | ||
public void Subtract_purely_real_numbers() | ||
{ | ||
var sut = new ComplexNumber(1, 0); | ||
var expected = new ComplexNumber(-1, 0); | ||
Assert.Equal(expected.Real(), sut.Sub(new ComplexNumber(2, 0)).Real(), 15); | ||
Assert.Equal(expected.Imaginary(), sut.Sub(new ComplexNumber(2, 0)).Imaginary(), 15); | ||
} | ||
|
||
[Fact(Skip = "Remove to run test")] | ||
public void Subtract_purely_imaginary_numbers() | ||
{ | ||
var sut = new ComplexNumber(0, 1); | ||
var expected = new ComplexNumber(0, -1); | ||
Assert.Equal(expected.Real(), sut.Sub(new ComplexNumber(0, 2)).Real(), 15); | ||
Assert.Equal(expected.Imaginary(), sut.Sub(new ComplexNumber(0, 2)).Imaginary(), 15); | ||
} | ||
|
||
[Fact(Skip = "Remove to run test")] | ||
public void Subtract_numbers_with_real_and_imaginary_part() | ||
{ | ||
var sut = new ComplexNumber(1, 2); | ||
var expected = new ComplexNumber(-2, -2); | ||
Assert.Equal(expected.Real(), sut.Sub(new ComplexNumber(3, 4)).Real(), 15); | ||
Assert.Equal(expected.Imaginary(), sut.Sub(new ComplexNumber(3, 4)).Imaginary(), 15); | ||
} | ||
|
||
[Fact(Skip = "Remove to run test")] | ||
public void Multiply_purely_real_numbers() | ||
{ | ||
var sut = new ComplexNumber(1, 0); | ||
var expected = new ComplexNumber(2, 0); | ||
Assert.Equal(expected.Real(), sut.Mul(new ComplexNumber(2, 0)).Real(), 15); | ||
Assert.Equal(expected.Imaginary(), sut.Mul(new ComplexNumber(2, 0)).Imaginary(), 15); | ||
} | ||
|
||
[Fact(Skip = "Remove to run test")] | ||
public void Multiply_purely_imaginary_numbers() | ||
{ | ||
var sut = new ComplexNumber(0, 1); | ||
var expected = new ComplexNumber(-2, 0); | ||
Assert.Equal(expected.Real(), sut.Mul(new ComplexNumber(0, 2)).Real(), 15); | ||
Assert.Equal(expected.Imaginary(), sut.Mul(new ComplexNumber(0, 2)).Imaginary(), 15); | ||
} | ||
|
||
[Fact(Skip = "Remove to run test")] | ||
public void Multiply_numbers_with_real_and_imaginary_part() | ||
{ | ||
var sut = new ComplexNumber(1, 2); | ||
var expected = new ComplexNumber(-5, 10); | ||
Assert.Equal(expected.Real(), sut.Mul(new ComplexNumber(3, 4)).Real(), 15); | ||
Assert.Equal(expected.Imaginary(), sut.Mul(new ComplexNumber(3, 4)).Imaginary(), 15); | ||
} | ||
|
||
[Fact(Skip = "Remove to run test")] | ||
public void Divide_purely_real_numbers() | ||
{ | ||
var sut = new ComplexNumber(1, 0); | ||
var expected = new ComplexNumber(0.5, 0); | ||
Assert.Equal(expected.Real(), sut.Div(new ComplexNumber(2, 0)).Real(), 15); | ||
Assert.Equal(expected.Imaginary(), sut.Div(new ComplexNumber(2, 0)).Imaginary(), 15); | ||
} | ||
|
||
[Fact(Skip = "Remove to run test")] | ||
public void Divide_purely_imaginary_numbers() | ||
{ | ||
var sut = new ComplexNumber(0, 1); | ||
var expected = new ComplexNumber(0.5, 0); | ||
Assert.Equal(expected.Real(), sut.Div(new ComplexNumber(0, 2)).Real(), 15); | ||
Assert.Equal(expected.Imaginary(), sut.Div(new ComplexNumber(0, 2)).Imaginary(), 15); | ||
} | ||
|
||
[Fact(Skip = "Remove to run test")] | ||
public void Divide_numbers_with_real_and_imaginary_part() | ||
{ | ||
var sut = new ComplexNumber(1, 2); | ||
var expected = new ComplexNumber(0.44, 0.08); | ||
Assert.Equal(expected.Real(), sut.Div(new ComplexNumber(3, 4)).Real(), 15); | ||
Assert.Equal(expected.Imaginary(), sut.Div(new ComplexNumber(3, 4)).Imaginary(), 15); | ||
} | ||
|
||
[Fact(Skip = "Remove to run test")] | ||
public void Absolute_value_of_a_positive_purely_real_number() | ||
{ | ||
var sut = new ComplexNumber(5, 0); | ||
Assert.Equal(5, sut.Abs()); | ||
} | ||
|
||
[Fact(Skip = "Remove to run test")] | ||
public void Absolute_value_of_a_negative_purely_real_number() | ||
{ | ||
var sut = new ComplexNumber(-5, 0); | ||
Assert.Equal(5, sut.Abs()); | ||
} | ||
|
||
[Fact(Skip = "Remove to run test")] | ||
public void Absolute_value_of_a_purely_imaginary_number_with_positive_imaginary_part() | ||
{ | ||
var sut = new ComplexNumber(0, 5); | ||
Assert.Equal(5, sut.Abs()); | ||
} | ||
|
||
[Fact(Skip = "Remove to run test")] | ||
public void Absolute_value_of_a_purely_imaginary_number_with_negative_imaginary_part() | ||
{ | ||
var sut = new ComplexNumber(0, -5); | ||
Assert.Equal(5, sut.Abs()); | ||
} | ||
|
||
[Fact(Skip = "Remove to run test")] | ||
public void Absolute_value_of_a_number_with_real_and_imaginary_part() | ||
{ | ||
var sut = new ComplexNumber(3, 4); | ||
Assert.Equal(5, sut.Abs()); | ||
} | ||
|
||
[Fact(Skip = "Remove to run test")] | ||
public void Conjugate_a_purely_real_number() | ||
{ | ||
var sut = new ComplexNumber(5, 0); | ||
var expected = new ComplexNumber(5, 0); | ||
Assert.Equal(expected.Real(), sut.Conjugate().Real(), 15); | ||
Assert.Equal(expected.Imaginary(), sut.Conjugate().Imaginary(), 15); | ||
} | ||
|
||
[Fact(Skip = "Remove to run test")] | ||
public void Conjugate_a_purely_imaginary_number() | ||
{ | ||
var sut = new ComplexNumber(0, 5); | ||
var expected = new ComplexNumber(0, -5); | ||
Assert.Equal(expected.Real(), sut.Conjugate().Real(), 15); | ||
Assert.Equal(expected.Imaginary(), sut.Conjugate().Imaginary(), 15); | ||
} | ||
|
||
[Fact(Skip = "Remove to run test")] | ||
public void Conjugate_a_number_with_real_and_imaginary_part() | ||
{ | ||
var sut = new ComplexNumber(1, 1); | ||
var expected = new ComplexNumber(1, -1); | ||
Assert.Equal(expected.Real(), sut.Conjugate().Real(), 15); | ||
Assert.Equal(expected.Imaginary(), sut.Conjugate().Imaginary(), 15); | ||
} | ||
|
||
[Fact(Skip = "Remove to run test")] | ||
public void Eulers_identity_formula() | ||
{ | ||
var sut = new ComplexNumber(0, Math.PI); | ||
var expected = new ComplexNumber(-1, 0); | ||
Assert.Equal(expected.Real(), sut.Exp().Real(), 15); | ||
Assert.Equal(expected.Imaginary(), sut.Exp().Imaginary(), 15); | ||
} | ||
|
||
[Fact(Skip = "Remove to run test")] | ||
public void Exponential_of_0() | ||
{ | ||
var sut = new ComplexNumber(0, 0); | ||
var expected = new ComplexNumber(1, 0); | ||
Assert.Equal(expected.Real(), sut.Exp().Real(), 15); | ||
Assert.Equal(expected.Imaginary(), sut.Exp().Imaginary(), 15); | ||
} | ||
|
||
[Fact(Skip = "Remove to run test")] | ||
public void Exponential_of_a_purely_real_number() | ||
{ | ||
var sut = new ComplexNumber(1, 0); | ||
var expected = new ComplexNumber(Math.E, 0); | ||
Assert.Equal(expected.Real(), sut.Exp().Real(), 15); | ||
Assert.Equal(expected.Imaginary(), sut.Exp().Imaginary(), 15); | ||
} | ||
} |
Oops, something went wrong.