Skip to content

Commit e4a6b94

Browse files
committed
use mathnet; support mul, inverse, power,transpose
1 parent 4604615 commit e4a6b94

File tree

63 files changed

+452857
-60
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+452857
-60
lines changed
45.5 KB
Binary file not shown.
240 KB
Binary file not shown.

MathMLToCSharp/Entities/Mrow.cs

Lines changed: 52 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,55 +2,65 @@
22

33
namespace MathMLToCSharp.Entities
44
{
5-
/// <summary>
6-
/// A fairly generic element that does not mean anything.
7-
/// </summary>
8-
public class Mrow : WithBuildableContents
9-
{
10-
public Mrow(IBuildable[] content) : base(content)
5+
/// <summary>
6+
/// A fairly generic element that does not mean anything.
7+
/// </summary>
8+
public class Mrow : WithBuildableContents
119
{
10+
public Mrow(IBuildable[] content) : base(content)
11+
{
1212

13-
}
13+
}
1414

15-
public Mrow(IBuildable first, IBuildable second) : this(new[] {first, second})
16-
{
17-
}
15+
public Mrow(IBuildable first, IBuildable second) : this(new[] { first, second })
16+
{
17+
}
1818

19-
public Mrow(IBuildable content) : this(new[] {content})
20-
{
21-
}
19+
public Mrow(IBuildable content) : this(new[] { content })
20+
{
21+
}
2222

23-
public IBuildable[] Contents
24-
{
25-
get { return contents; }
26-
}
23+
public IBuildable[] Contents
24+
{
25+
get { return contents; }
26+
}
2727

28-
/// <summary>
29-
/// Returns <c>true</c> if this <see cref="Mrow"/> contains a single <see cref="Mi"/>.
30-
/// </summary>
31-
public bool ContainsSingleMi
32-
{
33-
get
34-
{
35-
return contents.Length == 1 && contents[0] is Mi;
36-
}
37-
}
28+
/// <summary>
29+
/// Returns <c>true</c> if this <see cref="Mrow"/> contains a single <see cref="Mi"/>.
30+
/// </summary>
31+
public bool ContainsSingleMi
32+
{
33+
get
34+
{
35+
return contents.Length == 1 && contents[0] is Mi;
36+
}
37+
}
3838

39-
/// <summary>
40-
/// Gets a value indicating whether this Mrow contains a single Mn.
41-
/// </summary>
42-
/// <value><c>true</c> if this row contains a single Mn; otherwise, <c>false</c>.</value>
43-
public bool ContainsSingleMn
44-
{
45-
get
46-
{
47-
return contents.Length == 1 && contents[0] is Mn;
48-
}
49-
}
39+
/// <summary>
40+
/// Gets a value indicating whether this Mrow contains a single Mn.
41+
/// </summary>
42+
/// <value><c>true</c> if this row contains a single Mn; otherwise, <c>false</c>.</value>
43+
public bool ContainsSingleMn
44+
{
45+
get
46+
{
47+
return contents.Length == 1 && contents[0] is Mn;
48+
}
49+
}
5050

51-
public override void Visit(StringBuilder sb, BuildContext context)
52-
{
53-
base.Visit(sb, context);
51+
/// <summary>
52+
/// Returns <c>true</c> if this <see cref="Mrow"/> contains a single <see cref="Mtable"/>.
53+
/// </summary>
54+
public bool ContainsSingleMtable
55+
{
56+
get
57+
{
58+
return contents.Length == 1 && contents[0] is Mtable;
59+
}
60+
}
61+
public override void Visit(StringBuilder sb, BuildContext context)
62+
{
63+
base.Visit(sb, context);
64+
}
5465
}
55-
}
5666
}

MathMLToCSharp/Entities/Msup.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,29 @@ public override void Visit(StringBuilder sb, BuildContext bc)
3636
}
3737
else
3838
{
39+
if((first is Mrow) && ((first as Mrow).ContainsSingleMtable))
40+
{
41+
if((second is Mrow) && ((second as Mrow).ContainsSingleMi) &&((second as Mrow).LastElement as Mi).Content == "T")
42+
{
43+
first.Visit(sb, bc);
44+
sb.Append(".Transpose()");
45+
}
46+
else if ((second is Mrow) && ((second as Mrow).ContainsSingleMn) &&((second as Mrow).LastElement as Mn).IsIntegerGreaterThan1)
47+
{
48+
first.Visit(sb, bc);
49+
sb.Append(".Power(");
50+
second.Visit(sb, bc);
51+
sb.Append(")");
52+
}
53+
else if ((second is Mrow) && ((second as Mrow).ContainsSingleMn) && ((second as Mrow).LastElement as Mn).Content=="-1")
54+
{
55+
first.Visit(sb, bc);
56+
sb.Append(".Inverse()");
57+
}
58+
bc.Tokens.Add(this);
59+
return;
60+
}
61+
3962
if (bc.LastTokenRequiresTimes)
4063
sb.Append("*");
4164

@@ -55,7 +78,7 @@ public override void Visit(StringBuilder sb, BuildContext bc)
5578
for (int i = 0; i < power; ++i)
5679
{
5780
if (i != 0 && (first is Mrow) && ((first as Mrow).ContainsSingleMn))
58-
sb.Append("*"); //for mn^2 not appended * automatically
81+
sb.Append("*"); //for the case mn^2 not appended * automatically
5982
first.Visit(sb, bc); // * sign appended automatically
6083
}
6184
}

MathMLToCSharp/Entities/Mtable.cs

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,36 @@
11
using System.Text;
2+
using MathNet.Numerics.LinearAlgebra;
3+
using MathNet.Numerics.LinearAlgebra.Double;
24

35
namespace MathMLToCSharp.Entities
46
{
5-
/// <summary>
6-
/// A table. Typically used for matrices.
7-
/// </summary>
8-
public class Mtable : WithBuildableContents
9-
{
10-
public Mtable(IBuildable[] contents) : base(contents) {}
11-
public override void Visit(StringBuilder sb, BuildContext context)
7+
/// <summary>
8+
/// A table. Typically used for matrices.
9+
/// </summary>
10+
public class Mtable : WithBuildableContents
1211
{
13-
sb.Append("{");//matrix
14-
for (int i = 0; i < contents.Length; ++i)
15-
{
16-
contents[i].Visit(sb, context);
17-
if (i + 1 != contents.Length)
18-
sb.Append(", ");
19-
}
20-
sb.Append("}");
12+
static int level = 0;
13+
public Mtable(IBuildable[] contents) : base(contents) { }
14+
public override void Visit(StringBuilder sb, BuildContext context)
15+
{
16+
level++;
17+
Matrix<double> m = DenseMatrix.OfArray(new double[,] { { 1, 2 }, { 3, 4 } });
18+
var c = m.Determinant();
19+
if (level == 1)
20+
sb.Append("DenseMatrix.OfArray(new double[,] {");//matrix
21+
else
22+
sb.Append("{");//matrix
23+
for (int i = 0; i < contents.Length; ++i)
24+
{
25+
contents[i].Visit(sb, context);
26+
if (i + 1 != contents.Length)
27+
sb.Append(", ");
28+
}
29+
if (level == 1)
30+
sb.Append("})");
31+
else
32+
sb.Append("}");
33+
level--;
34+
}
2135
}
22-
}
2336
}

MathMLToCSharp/MathMLToCSharp.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@
4343
</PropertyGroup>
4444
<ItemGroup>
4545
<Reference Include="DenisVuyka.Controls.PropertyGrid, Version=1.0.0.0, Culture=neutral, PublicKeyToken=49a938f81cb86363, processorArchitecture=MSIL" />
46+
<Reference Include="MathNet.Numerics, Version=3.20.0.0, Culture=neutral, processorArchitecture=MSIL">
47+
<HintPath>packages\MathNet.Numerics.3.20.0\lib\net35\MathNet.Numerics.dll</HintPath>
48+
</Reference>
4649
<Reference Include="PowerCollections, Version=1.0.2141.24679, Culture=neutral, processorArchitecture=MSIL">
4750
<SpecificVersion>False</SpecificVersion>
4851
<HintPath>.\PowerCollections.dll</HintPath>
@@ -51,6 +54,9 @@
5154
<Reference Include="System.Core">
5255
<RequiredTargetFramework>3.5</RequiredTargetFramework>
5356
</Reference>
57+
<Reference Include="System.Threading, Version=1.0.2856.102, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
58+
<HintPath>packages\TaskParallelLibrary.1.0.2856.0\lib\Net35\System.Threading.dll</HintPath>
59+
</Reference>
5460
<Reference Include="System.Xml.Linq">
5561
<RequiredTargetFramework>3.5</RequiredTargetFramework>
5662
</Reference>
@@ -135,6 +141,7 @@
135141
<SubType>Designer</SubType>
136142
</EmbeddedResource>
137143
<None Include="app.config" />
144+
<None Include="packages.config" />
138145
<None Include="Properties\Settings.settings">
139146
<Generator>SettingsSingleFileGenerator</Generator>
140147
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
107 KB
Binary file not shown.
64 KB
Binary file not shown.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<configSections>
4+
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
5+
<section name="MathMLToCSharp.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
6+
</sectionGroup>
7+
</configSections>
8+
<userSettings>
9+
<MathMLToCSharp.Properties.Settings>
10+
<setting name="WindowState" serializeAs="String">
11+
<value>Normal</value>
12+
</setting>
13+
</MathMLToCSharp.Properties.Settings>
14+
</userSettings>
15+
</configuration>
138 KB
Binary file not shown.

0 commit comments

Comments
 (0)