Тексти програм реалізації функцій
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Lab1;
using Lab1.Lab2;
namespace Lab3
{
sealed class MetrizedRelation: PropertiedRelation
{
private int _size;
private int[,] wages;
public MetrizedRelation(int[,] wages)
{
_size = wages.GetLength(0);
this.wages = new int[_size,_size];
for (int i = 0; i < Size; i++)
{
for (int j = 0; j < Size; j++)
{
this.wages[i, j] = wages[i, j];
}
}
}
public MetrizedRelation(){}
public int GetWage(int i, int j)
{
return wages[i, j];
}
public bool IsConsistencied()
{
for (int i = 0; i < Size; i++)
{
for (int j = i; j < Size; j++)
{
if (wages[i,j] != wages[j,i])
return false;
}
}
return true;
}
public void SetWage(int i, int j, int value)
{
wages[i, j] = value;
}
public bool IsAditive()
{
bool res = IsTriangleRelation((a, b) => a + b);
return res;
}
public bool IsMultiplicive()
{
bool res = IsTriangleRelation((a, b) => a * b);
return res;
}
#region Helpers
private int[,] PerformOperation(MetrizedRelation other, Func<int, int, int> op)
{
var res = new int[Size, Size];
for (int i = 0; i < Size; i++)
{
for (int j = 0; j < Size; j++)
{
res[i, j] = op(GetWage(i, j), other.GetWage(i, j));
}
}
return res;
}
private bool CheckType(Relation other)
{
return other is MetrizedRelation;
}
private bool IsTriangleRelation(Func<int, int, int> op)
{
bool res = true;
for (int i = 0; i < Size; i++)
{
for (int j = 0; j < Size; j++)
{
if (IsInRelation(i, j))
{
for (int k = 0; k < Size; k++)
{
if (IsInRelation(j, k))
if (IsInRelation(i, k))
{
if (wages[i, k] != op(wages[i, j], wages[j, k]))
{
res = false;
}
}
}
}
}
}
return res;
}
#endregion
public override Relation Intersection(Relation other) // ^
{
if (!CheckType(other))
{
return new MatrixBasedRelation(this).Intersection(other);
};
if (IsMultiplicive())
{
return new MetrizedRelation(PerformOperation((MetrizedRelation)other, (a,b)=>(int)Math.Round(Math.Sqrt(a*b))));
}
else
{
return new MetrizedRelation(PerformOperation((MetrizedRelation) other, (a, b) => (a + b)/2));
}
}
public override Relation Union(Relation other)
{
if (!CheckType(other))
{
return new MatrixBasedRelation(this).Union(other);
};
if (IsMultiplicive())
{
return new MetrizedRelation(PerformOperation((MetrizedRelation) other, (a, b) =>
{
if (a!=0 && b==0)
{
return b;
}
else if (a==0 && b!=0)
{
return b;
}
else
{
return
(int)
(Math.Round(
Math.Sqrt(a*b)));
}
}));
}
else
{
return new MetrizedRelation(PerformOperation((MetrizedRelation) other, (a, b) =>
{
if (a != 0 && b == 0)
{
return b;
}
else if (a == 0 && b != 0)
{
return b;
}
else
{
return
(a + b)/2;
}
}));
}
} // U
public override Relation Distinction(Relation other) // \
{
if (!CheckType(other))
{
return new MatrixBasedRelation(this).Distinction(other);
};
return new MetrizedRelation(PerformOperation((MetrizedRelation)other, (a, b) => a));
}
public override Relation SymmetricDistinction(Relation other) // xor
{
if (!CheckType(other))
{
return new MatrixBasedRelation(this).SymmetricDistinction(other);
};
return new MetrizedRelation(PerformOperation((MetrizedRelation)other, (a, b) => a));
}
public override Relation Complemention() // ~
{
return new MetrizedRelation(PerformOperation(this, (a, b) => 1));
}
public override Relation Reverse() // -1
{
var newWages = new int[Size,Size];
for (int i = 0; i < Size; i++)
{
for (int j = 0; j < Size; j++)
{
newWages[i, j] = wages[j, i];
}
}
return new MetrizedRelation(newWages);
}
public override Relation Compose(Relation other)
{
if (!(other is MetrizedRelation))
throw new NotImplementedException();
var newMatrixArray = GetTransitionedMatrix(wages, (MetrizedRelation)other);
return new MetrizedRelation(newMatrixArray);
}
public override Relation Restriction()
{
throw new NotImplementedException();
}
public bool IsMultiplicievlyTransitive()
{
return IsMultiplicive() && IsTransitive();
}
public bool IsAditivelyTransitive()
{
return IsAditive() && IsTransitive();
}
private int CheckForTransition(int i, int j, int[,] thisMatrix, MetrizedRelation other)
{
for (int k = 0; k < thisMatrix.GetLength(0); k++)
{
if (thisMatrix[i, k] != 0 && other.IsInRelation(k, j))
{
if (IsMultiplicive())
{
return ((int)Math.Round(Math.Sqrt(thisMatrix[i, k] * other.GetWage(k, j))));
}
else
{
return ((thisMatrix[i, k] + other.GetWage(k, j)) / 2);
}
}
}
return 0;
}
private int[,] GetTransitionedMatrix(int[,] thisMatrix, MetrizedRelation other)
{
var transitioned = new int[thisMatrix.GetLength(0), thisMatrix.GetLength(1)];
for (int i = 0; i < transitioned.GetLength(0); i++)
{
for (int j = 0; j < transitioned.GetLength(1); j++)
{
if (thisMatrix[i, j]!=0 && other.IsInRelation(i, j))
{
if (IsMultiplicive())
{
transitioned[i, j] = ((int)Math.Round(Math.Sqrt(thisMatrix[i, j] * other.GetWage(i, j))));
}
else
{
transitioned[i, j] = (thisMatrix[i, j] + other.GetWage(i, j))/2;
}
}
else
{
transitioned[i, j] = CheckForTransition(i, j, thisMatrix, other);
}
}
}
return transitioned;
}
public override bool IsInRelation(int a, int b)
{
return wages[a, b] != 0;
}
public override int Size
{
get { return _size; }
}
public override void Show()
{
for (int i = 0; i < Size; i++)
{
for (int j = 0; j < Size; j++)
{
Console.Write("{0,3}", wages[i,j]);
}
Console.WriteLine();
}
}
public override bool IsSubRelation(Relation other)
{
var isMetrized = other is MetrizedRelation;
if (other.Size < this.Size)
return false;
for (int i = 0; i < other.Size; i++)
{
for (int j = 0; j < other.Size; j++)
{
if (other.IsInRelation(i, j))
{
if (!IsInRelation(i, j) ||
(isMetrized && (((MetrizedRelation)other).GetWage(i, j) != wages[i,j])))
return false;
}
}
}
return true;
}
public override Relation FactorizeByEquivalence(Relation relation)
{
return new SectionBasedRelation(this).FactorizeByEquivalence(relation);
}
public void Get()
{
var s = Console.ReadLine();
var splitted = s.Split(' ');
_size = splitted.Length;
wages = new int[Size,Size];
for (int i = 0; i < Size; i++)
{
if (i>0)
{
s = Console.ReadLine();
splitted = s.Split(' ');
}
for (int j = 0; j < Size; j++)
{
wages[i, j] = int.Parse(splitted[j]);
}
}
}
}
}