没有合适的资源?快使用搜索试试~ 我知道了~
BinMatrix:BinMatrix是用于操作和存储二进制矩阵的类-开源
0 下载量 22 浏览量
2021-04-29
10:57:15
上传
评论
收藏 765KB PDF 举报
温馨提示
试读
129页
BinMatrix是用于操作和存储二进制矩阵的类。 每个BinMatrix对象代表一个二进制矩阵。 支持的操作包括“或”,“与”,乘积和乘方等。 该类旨在在C ++程序中使用。
资源推荐
资源详情
资源评论
BinMatrix
version 3
Introduction.
BinMatrix is a class for store a binary matrix and operate between them.
Each BinMatrix-object represent a binary matrix. This matrix always is quadratic,
which size is stored inside member variable N. So, matrix have size NxN.
We have 4 levels of abstraction for a matrix, which main entry is BinMatrix :
- BinMatrix. Only maintain a variable for tell if the underling values are taking as
original, or we take as a NOT operation in front (NOT(0)=1, NOT(1)=0, for each
cell). This variable is propNot. This class will set and obtain values from a
BinMatrixA object.
- BinMatrixA. “BinMatrix Absolute”. Within this class, we can establish
permutations. Interface permit to set permutations. BinMatrixA get his values from a
permutated or not permutated matrix stored inside a object BinMatrixP. This 2
permute matrices are called permut1 and permut2, and a matrix BinMatrixA, are in
form : permut1 * M * permut2, in which M have values from BinMatrixP object.
- BinMatrixP. “BinMatrix Permutated”. Within this class, we can select rows and
columns to use over a specific background (examples are a background of zeros, or a
background of ones or a background of a lower triangle). Values from cells which
rows and columns are selected are looked at the “small matrix” or main object of type
BinMatrixR, in which we store data. If a cell have either a row or columns not
selected, his values need to take from background.
- BinMatrixR. “BinMatrix Recursive”. In this matrix-object we store all data. It's
recursive because it can contain inside many smaller BinMatrix-objects. This matrix
can be of 3 types : dense, sparse and partitioned. All matrices of BinMatrixR have a
size power of 2 (64,128,256, ...)
BinMatrix BinMatrixA BinMatrixP BinMatrixR
Each class can be used independently, but BinMatrix offer most of functions.
BinMatrix is also able to recognize triangular matrix, using transformations into his
BinMatrixA object. Also, BinMatrix need to have a constructor, for tell him that we
send a antisimetric data, so BinMatrix will automatically search for a triangular (it is
not a general case). A good idea in case of triangulars, is add a capability to
BinMatrixP to select diagonal rows instead of regular rows and columns. In this way,
it will spare more memory.
If you not use operation NOT, not use permutation and not use selected rows
capability, because you know its a very dense matrix, you can also directly construct
a BinMatrixR matrix, instead of a BinMatrix.
A diagram image :
Versioned background.
In version 1, i use a matrix with many different types of BinMatrixR and without a
BinMatrixP and BinMatrixA. Types include :
pEnd, pBinpart, pMulti, pZero, pOne, pIdentity, pSparse, pRowColumns, pData
After i see, in this way its too long to program (9*9 cases if we operate) i decide to
make a second version, which have only 3 :
BinMatrix
propNot
BinMatrixA
permut1
propNot
permut2
BinMatrixP
m_cProp
m_bigToSmallRow m_bigToSmallCol
BinMatrixR
pSparse
pSparse
pSparse
pEnd pSparse
pSparse
pEnd
pEnd, pBinpart, pSparse
I see pMulti (union of submatrices) is not practical. pData is only temporal, but very
inefficient. And all { pSparse, pZero, pOne, pIdentity } can be unified for one type
pSparse with differents background (zero-background or ones-background, ...). I take
pRowColumns concept outside of BinMatrixR, it will become BinMatrixP, because
only have sense in general to select rows and columns in a total way. BinMatrixR will
become very much simple.
In this third version, we do something still different to the version 2. For example
how we select rows and columns, store in memory. Also add a level of permutations
(BinMatrixA) because triangulars only have sense if we can permutate (in general
rows and columns not will have a perfect order). Also, in version 2 we handle inside
BinMatrixR, not quadratic matrices, and this let make a code more unreadibily and
more long to program. In version 3, this always are quadratic and size is a power of 2,
so it is very much easy to handle. A not quadratic matrix can be partitioned from a
bigger quadratic size, and a zero-submatrix, will be fast to multiply.
Another difference is also, pEnd is also always a quadratic size and its size is as big
as bits in a byte. So, in year 2014 we take this value (bmes) as 64, so a pEnd matrix
always have size 64x64.
In the next section we explain in more detail each class of matrix :
BigMatrix.
It is the main interface for all other matrix classes. Only maintain a variable for tell if
the underling values are taking as original, or we take as a NOT operation in front
(NOT(0)=1, NOT(1)=0, for each cell). This variable is propNot. This class will set
and obtain values from a BinMatrixA object. A setNot() will only change propNot.
If propNot==false , we not have applied a NOT operation, but if it is true, we applied
a NOT operation. In cases of operations between 2 matrices, in some cases will force
propNot to be true or false, changing underling matrix objects to reflect this.
Main operations.
We have 2 main operations : one for set values, and another for get values :
• void setValue(int a,int b,bool _value);
• bool value(int a,int b) const;
Operations.
We have defined 3 types of operations between matrices and one with integer :
- Sume of matrices. In reality is to take 2 matrices of same size and realize a
operation OR between between cell (i,j) of first matrix and cell (i,j) of second matrix,
and put result in cell (i,j) of resulting matrix.
We have defined this functions for this :
• BinMatrix& operator+=(const BinMatrix& m);
• BinMatrix& opOr(const BinMatrix& bm2);
• static void applyOr(BinMatrix& A, const BinMatrix& B);
• static void opOr(BinMatrix& A, BinMatrix& B);
Recommended is use of form opOr, because all operations have this, in case of 2
operands. Also it is pendent to do a
static void opOr(BinMatrix& C,BinMatrix& A, BinMatrix& B);
In which we store result inside C.
- Hadamard product. It is realize a operation AND between 2 matrices to return a
result, in a similar way to sume of matrices.
We have defined this functions :
• BinMatrix& opAnd(const BinMatrix& bm2);
• static void opAnd(BinMatrix& A, BinMatrix& B);
- Operations XOR and XNOR. We have this operations , this functions :
• static void opXor(BinMatrix& A, BinMatrix& B);
• static void opXnor(BinMatrix& A, BinMatrix& B);
Always , it will take A and B to realize A XOR B and put result into A.
- Multiplication. It realize a multiplication wetween two matrices. Supported
functions are :
• BinMatrix& operator*=(const BinMatrix& m);
• BinMatrix& opMult(const BinMatrix& bm2);
• static void opMult(BinMatrix& A,BinMatrix& B);
- Power. It realize a power of a matrix M : C = M^p. Here C is result. We only have
defined cased in which C will be same matrix M : M = M^p. Functions are :
• BinMatrix& operator^=(int power);
• BinMatrix& opPow(int power);
Until November its implemented to have a speed of <= 2*log
2
(power)
multiplications.
Set-Functions.
This are functions, to set a matrix in a determined way, without a second operand. We
have :
- setNot() . Make a NOT operations of each cell. Simply change propNot value.
- setTransposed(). Set this matrix to his transposed version. We not have a
propTransposed value, but this operations is very quicky, in time O(1)...O(log(N)),
because change background inside BinMatrixP is O(1), and transpose BinMatrixR
underling matrices of type BinMatrixE can be transposed in time O(1) and sparse
matrices also. Only in case we have a big matrix with many BinMatrixR submatrices,
this time can be increased to O(log(N)).
- setToZeros(). Simply set this matrix to a zero matrix.
- Get a inverse matrix : First check if this matrix have a inverse matrix, calling to
haveInverse() , and after, if have inverse, get the transposed matrix with
setTransposed(). Transposed matrix are same as inversed matrix in case of binary
matrix. For example, you want to get a inversed matrix of A, into B, we can do :
if (A.haveInverse()) { B = A; B.setTransposed(); }
Random functions.
We have 3 functions for set random values for a matrix :
• void setRandomValues(int randPoints, bool value=true);
• void setRandomValues(int fractNum, int fractDenomPow2);
• void setRandomValues(float prob);
First function set randPoints cells to value value. This is recommend for not dense
matrices, because it not guaranty to not repeat a same cells.
For set random dense matrices, use second or third function. Your only can use in
second function, denominators which are powers of 2 (like 2,4,8,16,...). For example,
your will set random values in which ones are 3 times more often in average as zeros.
So the probability of have a one is 3/4. For have this, call second function with :
A.setRandomValues(3,4);
If you want only a 3/8 of ones (and 5/8 of ones), then use :
A.setRandomValues(3,8);
If you not like to think about denominators, you can also use a third function, and if
your for example like a probability of 0.73 to have a one in a cell, you can call :
A.setRandomValues(0.73);
剩余128页未读,继续阅读
资源评论
刘怒威
- 粉丝: 26
- 资源: 4651
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功