forked from microsoft/QuantumKatas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BooleanMatrix.cs
171 lines (138 loc) · 4.53 KB
/
BooleanMatrix.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
//////////////////////////////////////////////////////////////////////
// This file contains an underlying implementation of a matrix.
// You should not modify anything in this file.
// The tasks themselves can be found in Tasks.qs file.
//////////////////////////////////////////////////////////////////////
namespace Quantum.Kata.SimonsAlgorithm
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class BooleanVector : IEnumerable<bool>, IComparable<BooleanVector>, IEquatable<BooleanVector>
{
private readonly List<bool> values;
public BooleanVector(int size)
{
values = new List<bool>(size);
}
public BooleanVector(IEnumerable<bool> array)
{
values = new List<bool>();
values.AddRange(array);
}
public BooleanVector(IEnumerable<long> array)
: this(array.Select(v => v != 0))
{
}
public bool this[int index]
{
get => values[index];
set => values[index] = value;
}
public static BooleanVector operator +(BooleanVector vector1, BooleanVector vector2)
{
if (vector1.Count != vector2.Count)
{
throw new ArgumentException($"Vectors have different sizes [{vector1.Count} != {vector2.Count}]");
}
return new BooleanVector(vector1.Zip(vector2, (val1, val2) => val1 ^ val2));
}
public static BooleanVector operator -(BooleanVector vector1, BooleanVector vector2)
{
return vector1 + vector2;
}
public IEnumerator<bool> GetEnumerator()
{
return values.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return values.GetEnumerator();
}
public int CompareTo(BooleanVector other)
{
int length = Math.Min(Count, other.Count);
for (int i = 0; i < length; i++)
{
var comp = this[i].CompareTo(other[i]);
if (comp != 0) return comp;
}
return Count.CompareTo(other.Count);
}
public int Count => values.Count;
public bool Equals(BooleanVector other)
{
return CompareTo(other) == 0;
}
#region Overrides of Object
public override string ToString()
{
return string.Join(", ", this);
}
#endregion
}
public class BooleanMatrix
{
private readonly List<BooleanVector> matrix = new List<BooleanVector>();
public int Height { get; }
public int? Width { get; }
public BooleanMatrix(BooleanMatrix matrix)
{
foreach (var vector in matrix.matrix)
{
this.matrix.Add(new BooleanVector(vector));
}
Height = matrix.Height;
Width = matrix.Width;
}
public BooleanMatrix(IEnumerable<BooleanVector> rows)
{
foreach (var row in rows)
{
matrix.Add(row);
if (Width == row.Count || Width == null)
{
Width = row.Count;
}
else
{
throw new ArgumentException("Not every row has the same size");
}
}
Height = matrix.Count;
}
public BooleanMatrix(IEnumerable<IEnumerable<long>> source)
{
foreach (var row in source)
{
matrix.Add(new BooleanVector(row.Select(x => x != 0)));
Width = matrix.Last().Count;
}
Height = matrix.Count;
}
public BooleanMatrix(IEnumerable<IEnumerable<bool>> rows)
: this(rows.Select(row => new BooleanVector(row)))
{
}
public BooleanVector this[int index]
{
get => matrix[index];
set => matrix[index] = value;
}
public List<BooleanVector> GetKernel()
{
if (Width == null)
{
throw new ArgumentNullException(nameof(Width));
}
return GetKernel(Width.Value - 1);
}
public List<BooleanVector> GetKernel(int minimalRank)
{
return GaussianElimination.GetKernel(this, minimalRank);
}
}
}