Skip to content

Commit e2acd34

Browse files
committed
1 parent 876667b commit e2acd34

File tree

4 files changed

+69
-10
lines changed

4 files changed

+69
-10
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace MathExtended.Exceptions
8+
{
9+
/// <summary>
10+
/// Возникает при попытке операции
11+
/// над вектор строками/столбцами разных размеров
12+
/// </summary>
13+
public class VectorsDifferentSizeException : Exception
14+
{
15+
private string _message = "Vector row/column of different sizes";
16+
17+
/// <summary>
18+
/// Сообщение исключения
19+
/// </summary>
20+
public override string Message => _message;
21+
}
22+
}

MathExtended.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@
190190
<Reference Include="System.Xml" />
191191
</ItemGroup>
192192
<ItemGroup>
193+
<Compile Include="Exceptions\VectorsDifferentSizeException.cs" />
193194
<Compile Include="Matrices\Structures\CellsCollections\BaseCellsCollection.cs" />
194195
<Compile Include="Matrices\BaseMatrix.cs" />
195196
<Compile Include="Matrices\Structures\CellsCollections\BaseReadOnlyCellsCollection.cs" />

Matrices/Structures/Columns/Column.cs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,40 @@ public static implicit operator Column<T>(T[] array)
118118
}
119119
}
120120

121+
/// <summary>
122+
/// Умножает столбец на строку
123+
/// </summary>
124+
/// <param name="column">Столбец</param>
125+
/// <param name="row">Строка</param>
126+
/// <returns><see cref="Matrix{T}"/> результат умножения</returns>
127+
public static Matrix<T> operator *(Column<T> column, Row<T> row)
128+
{
129+
if (column.Size != row.Size)
130+
{
131+
throw new VectorsDifferentSizeException();
132+
}
133+
else
134+
{
135+
Matrix<T> matrix = new Matrix<T>(row.Size, column.Size);
136+
137+
for (int i = 0; i < row.Size; i++)
138+
{
139+
for (int j = 0; j < column.Size; j++)
140+
{
141+
matrix[i, j] = (T)Operator.Multiply(row[j], column[i]);
142+
}
143+
}
144+
145+
return matrix;
146+
}
147+
}
148+
149+
121150
/// <summary>
122151
/// Явно приводит <see cref="ReadOnlyColumn{T}"/> к <see cref="Column{T}"/>
123152
/// Делая пригодным для записи
124153
/// </summary>
125-
/// <param name="readOnlyColumn">Привидимый столбец</param>
154+
/// <param name="readOnlyColumn">Приводимый столбец</param>
126155
public static explicit operator Column<T>(ReadOnlyColumn<T> readOnlyColumn)
127156
{
128157
Column<T> column = new Column<T>(readOnlyColumn.Size);

Matrices/Structures/Rows/Row.cs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -111,26 +111,33 @@ public static implicit operator Row<T>(T[] array)
111111
}
112112

113113
/// <summary>
114-
/// Умножает строку на столбец
114+
/// Умножает строку на стотбец, получая число
115115
/// </summary>
116116
/// <param name="row">Строка</param>
117117
/// <param name="column">Столбец</param>
118-
/// <returns><see cref="Matrix{T}"/> результат умножения</returns>
119-
public static Matrix<T> operator *(Row<T> row, Column<T> column)
118+
/// <returns><typeparamref name="T"/> число</returns>
119+
public static T operator *(Row<T> row, Column<T> column)
120120
{
121-
Matrix<T> matrix = new Matrix<T>(row.Size,column.Size);
122-
123-
for(int i = 0;i < row.Size;i++)
121+
if (row.Size != column.Size)
122+
{
123+
throw new VectorsDifferentSizeException();
124+
}
125+
else
124126
{
125-
for(int j = 0;j < column.Size;j++)
127+
dynamic num = 0;
128+
129+
for (int i = 0; i < row.Size; i++)
126130
{
127-
matrix[i, j] = (T)Operator.Multiply(row[i],column[j]);
131+
num += (T)Operator.Multiply(row[i], column[i]);
128132
}
133+
134+
return num;
129135
}
130136

131-
return matrix;
137+
132138
}
133139

140+
134141
/// <summary>
135142
/// Приводит <see cref="ReadOnlyRow{T}"/> к <see cref="Row{T}"/>
136143
/// делая возможной запись

0 commit comments

Comments
 (0)