//--------------------------------------------//
// Especificacion Algebraica TAD Matriz //
// Autor: Pablo Sanchez (p.sanchez@unican.es) //
// http://personales.unican.es/sanchezbp //
//--------------------------------------------//

espec Matrix

usa Boolean, Natural

generos Matrix(Element) como Matrix(E)

parametro
generos Element como E
operaciones
equal: Element Element -> Boolean
variables
x, y, z : Element;
ecuaciones
equal(x,x) = TRUE
equal(x,y) = equal(y,x)
[equal(x,y) and equal(y,z)] equal(x,z) = TRUE;
fparametro

operaciones
parcial newMatrix : Natural Natural -> Matrix(E)
parcial set : Matrix(E) Natural Natural E -> Matrix(E)
parcial get : Matrix(E) Natural Natural -> E
equal : Matrix(E) Matrix(E) -> Boolean
rows : Matrix(E) -> Natural
columns : Matrix(E) -> Natural
parcial isSet : Matrix(E) Natural Natural -> Boolean
parcial resize : Matrix(E) Natural Natural -> Matrix(E)
sameSize: Matrix(E) Matrix(E) -> Boolean

// Funciones auxiliares
parcial equalElementByElementUntil:
Matrix(E) Matrix(E) Natural Natural -> Boolean

vars
m1, m2 : Matrix(E); x, y : Element;
i1, j1, i2, j2, n, m, n2, m2 : Natural;

precondiciones
[(0 <= i < rows(v)) and (0 <= j < columns(v))] set(m1,i,j,e)
[(0 <= i < rows(v)) and (0 <= j < columns(v))
and (isSet(m1,i,j) == TRUE)] get(m1,i,j)
[(0 < n) and (0 < m)] newMatrix(n,m)
[(n >= rows(m1)) and (m >= columns(m1))] resize(m1,n,m)
[(0 <= n < rows(m1)) and (0 <= m < columns(m1)) and
(0 <= n < rows(m2)) and (0 <= m < columns(m2))]
equalElementByElementUntil(m1,m2,n,m)

ecuaciones
// Las generadoras son newMatrix(n,m) y set(m1,i,,j,e) y no
// son libres
// Los patrones del tipo son:
// - newMatrix(n,m)
// - set(m1,i,j,e)

[(i1 == i2) and (j1 == j2)]
set(set(m1,i1,j1,x),i2,j2,y) = set(m1,i1,j1,y)
[NOT((i1 == i2) and (j1 == j2))]
set(set(m1,i1,j1,x),i2,j2,y) =
set(set(m1,i2,j2,y),i1,j1,x)

[(i1 == i2) and (j1 == j2)]
get(set(m1,i1,,j1,x),i2,j2) = x
[NOT((i1 == i2) and (j1 == j2))]
get(set(m1,i1,,j1,x),i2,j2) = get(m1,i2,j2)

isSet(newMatrix(n,m),i1,j1) = FALSE
[(i1 == i2) and (j1 == j2)]
isSet(set(m1,i1,j1,x),i2,j2) = TRUE
[NOT((i1 == i2) and (j1 == j2))]
isSet(set(m1,i1,j1,x),i2,j2) = isSet(m1,i2,j2)

rows(newMatrix(n,m)) = n
rows(set(m1,i1,j1,x)) = rows(m1)

columns(newMatrix(n,m)) = m
columns(set(m1,i1,j1,x)) = columns(m1)

resize(newMatrix(n,m),n2,m2) = newMatrix(n2,m2)
resize(set(m1,i1,j1,x),n2,m2)) = set(resize(m1,n2,m2),i1,j1,x)

sameSize(m1,m2) = and(equal(rows(m1),rows(m2)),
equal(columns(m1),columns(m2)))

[NOT(sameSize(m1,m2))]
equal(m1,m2) = FALSE
[sameSize(m1,m2)]
equal(m1,m2) =
equalElementByElementUntil(m1,m2,rows(m1)-1,
columns(m1)-1)

[(n == 0) and (m == 0)]
equalElementByElementUntil(m1,m2,n,m) =
or(
and(isSet(m1,0,0),isSet(m2,0,0),
equal(get(m1,0,0),get(m2,0,0))),
and(not(isSet(m1,0,0)),not(isSet(m2,0,0)))
)

[(n >= 0) and (m > 0))]
equalElementByElementUntil(m1,m2,n,m) =
and(
or(
and(isSet(m1,n,m),isSet(m2,n,m),
equal(get(m1,n,m),get(m2,n,m))),
and(not(isSet(m1,n,m)),not(isSet(m2,n,m)))
),
equalElementByElementUntil(m1,m2,n,m-1))

[(n > 0) and (m == 0))]
equalElementByElementUntil(m1,m2,n,m) =
and(
or(
and(isSet(m1,n,m),isSet(m2,n,m),
equal(get(m1,n,m),get(m2,n,m))),
and(not(isSet(m1,n,m)),not(isSet(m2,n,m)))
),
equalElementByElementUntil(m1,m2,n-1,
min(columns(m1),columns(m2)))

fespec

Última modificación: viernes, 26 de mayo de 2017, 11:19