Example #1
0
void restrictedOptimizationHandler::simplex<Type>::simplexOptimization(const LinAlg::Matrix<Type> &c,
                                                                       const LinAlg::Matrix<Type> &A,
                                                                       const LinAlg::Matrix<Type> &b,
                                                                       LinAlg::Matrix<Type> &B,
                                                                       LinAlg::Matrix<Type> &N)
{
//    %% selecione uma base B e calcule B^-1
    LinAlg::Matrix<Type> An = A(from(1)-->A.getNumberOfRows(),N);
    LinAlg::Matrix<Type> Ab = A(from(1)-->A.getNumberOfRows(),B);
//    std::cout << N << "\n";
    LinAlg::Matrix<Type> Abinv = LinAlg::inv_numeric(Ab);
//    LinAlg::Matrix<Type> Abinv = (Ab^-1);
    LinAlg::Matrix<Type> xb;
//    %% Verifique o vetor dos custos reduzidos
    for(unsigned terminate = 1; terminate <= 100; ++terminate)
    {
        LinAlg::Matrix<Type> cn = c(N,1);
        LinAlg::Matrix<Type> cb = c(B,1);
        LinAlg::Matrix<Type>An = A(from(1)-->A.getNumberOfRows(),N);
        Ab = A(from(1)-->A.getNumberOfRows(),B);
        LinAlg::Matrix<Type> r = (~cn) - (~cb)*Abinv*An;
        this->cost = ((~cb)*Abinv*b)(1,1);
        xb = Abinv*b;
        unsigned contFlag = 0;
        for (unsigned i=1; i <= r.getNumberOfColumns(); ++i)
           if(r(1,i) >= 0)
           {
               contFlag = contFlag + 1;
//%               break;
           }
        if(contFlag == r.getNumberOfColumns())
            break;
        //%determine o vetor de chegada
        unsigned indMinD = LinAlg::lineOfMinValue(~r);

        this->x = Abinv*An(from(1)-->An.getNumberOfRows(),indMinD);
        LinAlg::Matrix<Type> d = LinAlg::divPoint(xb,this->x);
        Type dMin = INFINITY;
        unsigned indMinB = 1;
        for (unsigned i = 1; i <= d.getNumberOfRows(); ++i)
           if(this->x(i,1) > 0 && d(i,1) < dMin)
           {
               dMin = d(i,1);
               indMinB = i;
           }

        Abinv = EscalSimplex((Abinv | xb | this->x),indMinB);
        Type Bout = B(1,indMinB);
        Type Nout = N(1,indMinD);
        N = N(1,from(1)-->(indMinD-1)) | Bout | N(1, from(indMinD+1)-->(N.getNumberOfColumns()));
        B = B(1,from(1)-->(indMinB-1)) | Nout | B(1,from(indMinB+1)-->(B.getNumberOfColumns()));
    }

    this->x = xb || LinAlg::Zeros<Type>(An.getNumberOfColumns(),1);
    LinAlg::Matrix<Type> ind = LinAlg::sortColumnVectorIndices<Type>(B|N);
    this->x = this->x(ind,1);
//    std::cout << "\n\n" << ind << "\n\n";
    N = LinAlg::sortColumnVector<Type>(N);
    B = LinAlg::sortColumnVector<Type>(B);
}
LinAlg::Matrix<float> SistemasLineares::Gauss(LinAlg::Matrix<float> MatrizUni)
{
    LinAlg::Matrix<float> MatrizGauss;
    //Laço para contagem das colunas de MatrizUni.
    for(unsigned i = 1; i < MatrizUni.getNumberOfColumns(); i++)
    {   //Laço para contagem das linhas de MatrizUni.
        for(unsigned j = i + 1; j <= MatrizUni.getNumberOfRows();  j++)
        {
            float m = MatrizUni(j,i)/MatrizUni(i,i);
            //Laço para contagem das colunas da equação.
            for(unsigned z = i ; z <= MatrizUni.getNumberOfColumns(); z++)
                MatrizUni(j,z) = MatrizUni(j,z) - m*MatrizUni(i,z);
        }
    }

    MatrizGauss = LinAlg::Zeros<float>(1, MatrizUni.getNumberOfRows());
    float R;
    for(unsigned i = 1; i <= MatrizUni.getNumberOfRows(); ++i)
    {
        unsigned k = MatrizUni.getNumberOfRows() - i + 1;
        R = 0;
        for(unsigned j = k + 1; j <= MatrizUni.getNumberOfColumns() - 1; ++j)
                R = R + MatrizUni(k, j) * MatrizGauss(1, j);

        MatrizGauss(1, k) = (MatrizUni(k, MatrizUni.getNumberOfColumns()) - R) / MatrizUni(k, k);
    }
    return MatrizGauss;
}
LinAlg::Matrix<float> SistemasLineares::GaussJacobi(LinAlg::Matrix<float> MatrizUni, unsigned MaxIterations)
{
    //Matriz Resposta
    LinAlg::Matrix<float> MatrizRes(MaxIterations, MatrizUni.getNumberOfColumns());
    LinAlg::Matrix<float> C (MatrizUni.getNumberOfRows(), MatrizUni.getNumberOfColumns() - 1);
    LinAlg::Matrix<float> g (MatrizUni.getNumberOfRows(), 1);
    LinAlg::Matrix<float> x0(C.getNumberOfColumns(), 1);

    //    //Deixa o vetor de chute inicial padronizado como vetor linha
    if(this->X0.getNumberOfColumns() < this->X0.getNumberOfRows())
        ~this->X0;

    //    //Insere o chute inicial na Matriz resposta
    for(unsigned i = 1; i < MatrizRes.getNumberOfColumns() - 1; ++i)
        x0(1,i) = this->X0(1,i);

        //Laço para contar as linhas da MatrizUni e Matriz C.
        for(unsigned i = 1; i <= MatrizUni.getNumberOfRows(); ++i)
        {   //Laço para contar as colunas da MAtrizUni e Matriz C.
            for(unsigned j = 1; j < MatrizUni.getNumberOfColumns(); ++j)
            {
                if(i != j)
                    C(i,j) = - MatrizUni(i,j)/MatrizUni(i,i);//Matriz com a diagonal zerada.
            }
            g(i,1) = MatrizUni(i,MatrizUni.getNumberOfColumns()) / MatrizUni(i,i);//Matriz dos termos independentes.
        }

        MatrizRes = ~x0;
        for(unsigned k = 1; k < MaxIterations; ++k)
        {
            x0 =  (C * x0) + g;
            MatrizRes = MatrizRes || ~x0;
        }
    return MatrizRes;
}
LinAlg::Matrix<Type> LinAlg::Matrix<Type>::operator|| (LinAlg::Matrix<RightType> rhs)
{
    LinAlg::Matrix<Type>ret;

    if(this->mat == NULL)
        ret = rhs;
    else
    {
        unsigned aux = this->rows;

        if(this->columns < rhs.getNumberOfColumns())
            ret.Init(this->rows + rhs.getNumberOfRows(), rhs.getNumberOfColumns());
        else
            ret.Init(this->rows + rhs.getNumberOfRows(), this->columns);

        for(unsigned i = 0; i < this->rows; i++)
            for(unsigned j = 0; j < this->columns; j++)
                ret.mat[i][j] = this->mat[i][j];

        for(unsigned i = 1; i <= rhs.getNumberOfRows(); i++)
            for(unsigned j = 1; j <= rhs.getNumberOfColumns(); j++)
                ret(i + aux, j) = rhs(i, j);
    }

    return ret;
}
void PolynomHandler::Polynom<Type>::setDen(LinAlg::Matrix<Type> Den)
{
    this->den = initPointer<Type>(Den.getNumberOfColumns());
    this->sizeDen = Den.getNumberOfColumns();

    for (unsigned i = 0; i < Den.getNumberOfColumns(); ++i)
        this->den[i] = Den(1,i+1);
}
void PolynomHandler::Polynom<Type>::setNum(LinAlg::Matrix<Type> Num)
{
    this->num = initPointer<Type>(Num.getNumberOfColumns());
    this->sizeNum = Num.getNumberOfColumns();

    for (unsigned i = 0; i < Num.getNumberOfColumns(); ++i)
        this->num[i] = Num(1,i+1);
}
LinAlg::Matrix<Type> LinAlg::operator~ (LinAlg::Matrix<Type> mat)
{
    LinAlg::Matrix<Type> temp(mat.getNumberOfColumns(), mat.getNumberOfRows());

    for(unsigned i = 1; i <= mat.getNumberOfRows(); i++)
        for(unsigned j = 1; j <= mat.getNumberOfColumns(); j++)
            temp(j, i) = mat(i, j);

    return temp;
}
void PolynomHandler::Polynom<Type>::init(LinAlg::Matrix<Type> Num)
{
    this->sizeNum = Num.getNumberOfColumns();
    this->num = initPointer(Num.getNumberOfColumns());
    for (unsigned i = 0; i < Num.getNumberOfColumns(); ++i)
        this->num[i] = (Type) Num(1, i+1);

    this->sizeDen = 1;
    this->den = initPointer<Type>(1);
    this->den[0] = 1;
    this->x = 'x';

}
Example #9
0
void PlotHandler::plot<Type>::generalPlot(LinAlg::Matrix<Type> X, LinAlg::Matrix<Type> Y)
{
//    centralWidget = new QWidget(this->properties.MainWindow);
//    centralWidget->setGeometry(QRect(this->properties.windowPosX, this->properties.windowPosY, this->properties.windowSizeX, this->properties.windowSizeY));
    customPlot = new QCustomPlot(this->properties.PlotFrame);
    customPlot->setGeometry(QRect(0, 0,this->properties.PlotFrame->geometry().width(), this->properties.PlotFrame->geometry().height()));

    // add title layout element:
    if(this->properties.titleFlag)
    {
        customPlot->plotLayout()->insertRow(0);
        customPlot->plotLayout()->addElement(0, 0, new QCPPlotTitle(this->customPlot, this->properties.title.c_str()));
    }

    if(X.getNumberOfColumns() == Y.getNumberOfColumns() && X.getNumberOfRows() == Y.getNumberOfRows())
    {
        this->properties.rows = X.getNumberOfRows();
        this->properties.columns = X.getNumberOfColumns();
//        this->setLegend();

        QPen pen;
        // add graphs with different scatter styles:
        for (unsigned i = 0; i < this->properties.rows; ++i)
        {
          customPlot->addGraph();
          pen.setColor(QColor(qSin(i*0.3)*100+100, qSin(i*0.6+0.7)*100+100, qSin(i*0.4+0.6)*100+100));
          // generate data:
          QVector<double> x(this->properties.columns), y(this->properties.columns);
          for (unsigned k = 0; k < this->properties.columns; ++k)
          {
            x[k] = X(i+1,k+1);
            y[k] = Y(i+1,k+1);
          }
          customPlot->graph()->setData(x, y);
          customPlot->graph()->rescaleAxes(true);
          customPlot->graph()->setPen(pen);

          if(this->properties.variablesNameFlag)
              customPlot->graph()->setName("Grafico" + QString::number(i+1));

          customPlot->graph()->setLineStyle(QCPGraph::lsLine);

          if(this->properties.xLabelFlag)
              customPlot->xAxis->setLabel(this->properties.xLabel.c_str());
          if(this->properties.yLabelFlag)
              customPlot->yAxis->setLabel(this->properties.yLabel.c_str());
          // set scatter style:
        }
     }
    customPlot->rescaleAxes();
}
LinAlg::Matrix<Type> PolynomHandler::MultPoly(const LinAlg::Matrix<Type> &lhs, const LinAlg::Matrix<Type> &rhs)
{
    unsigned lhsSize = lhs.getNumberOfColumns();
    unsigned rhsSize = rhs.getNumberOfColumns();
    LinAlg::Matrix<Type>ret(1,lhsSize+rhsSize+1);

    for(unsigned i = 0; i < lhsSize; ++i)
        for(unsigned j = 0; j < rhsSize; ++j)
        {
            ret(1,i+j+1) = ret(1,i+j+1) +  lhs(1,i+1)*rhs(1,j+1);
        }

    return ret;
}
void LinAlg::Balance (LinAlg::Matrix<Type> &matrix_to_balance)
{
    unsigned aux = 0;
    Type radix = FLT_RADIX, sqrdx = radix*radix, s, r, g, f, c;

    while(aux == 0)
    {
        aux = 1;
        for(unsigned i = 1; i <= matrix_to_balance.getNumberOfRows(); i++)
        {
            r = c = 0.0;
            for(unsigned j = 1; j <= matrix_to_balance.getNumberOfColumns(); j++)
                if( j != i)
                {
                    c += std::fabs(matrix_to_balance(j, i));
                    r += std::fabs(matrix_to_balance(i, j));
                }
            if(c && r)
            {
                g = r/radix;
                f = 1.0;
                s = c + r;
                while(c < g)
                {
                    f *= radix;
                    c *= sqrdx;
                }

                g = r*radix;
                while(c > g)
                {
                    f /= radix;
                    c /= sqrdx;
                }
                if((c + r)/f < 0.95*s)
                {
                    aux = 0;
                    g = 1.0/f;
                    for(unsigned j = 1; j <= matrix_to_balance.getNumberOfColumns(); j++)
                    {
                        matrix_to_balance(i, j) *= g;
                        matrix_to_balance(j, i) *= f;
                    }
                }
            }
        }
    }

}
Type LinAlg::Trace (const LinAlg::Matrix<Type>& mat)
{
    Type ret = 0;

    if(mat.getNumberOfRows() != mat.getNumberOfColumns())
        std::cout << "O traco so e calculado para matrizes quadradas.";
    else
    {
        for(unsigned i = 1; i <= mat.getNumberOfRows(); i++)
            for(unsigned j = 1; j <= mat.getNumberOfColumns(); j++)
                if(i == j)
                    ret += mat(i, j);
    }

    return ret;
}
void SistemasLineares::LU_Factorization(LinAlg::Matrix<float> Matriz, LinAlg::Matrix<float> &L, LinAlg::Matrix<float> &U)//Para Matriz Quadrada.
{
    L = LinAlg::Eye<float>(Matriz.getNumberOfRows());

    for(unsigned i = 1; i < Matriz.getNumberOfColumns(); ++i)
    {   //Laço para contagem das linhas de MatrizUni.
        for(unsigned j = i + 1; j <= Matriz.getNumberOfRows();  ++j)
        {
            L(j, i) = Matriz(j, i)/Matriz(i, i);
            //Laço para contagem das colunas da equação.
            for(unsigned z = i ; z <= Matriz.getNumberOfColumns(); ++z)
                Matriz(j, z) = Matriz(j, z) - L(j, i)*Matriz(i, z);
        }
    }
    U = Matriz;
}
LinAlg::Matrix<Type> LinAlg::CaracteristicPolynom (const LinAlg::Matrix<Type>& mat)
{
    unsigned n = mat.getNumberOfColumns();
    Matrix<Type> z = EigenValues(mat);
    Matrix<Type> zi = EigenValues(mat);
    Matrix<Type> ret(1,n+1);
    std::complex<Type> *tempPoly = new std::complex<Type> [2];
    tempPoly[0] = 1;
    tempPoly[1] = std::complex<Type>(-z(1,1),-z(1,2));
    std::complex<Type> * tempPolyEigenvalue = new std::complex<Type>[2];

    unsigned sizeTempPoly = 2;
    tempPolyEigenvalue[0] = 1;
    for(unsigned i = 2; i <= n ; ++i)
    {
        tempPolyEigenvalue[1] = std::complex<Type>(-z(i,1),-z(i,2));//apos o templade entre (real,imaginario) atribuição
        tempPoly = LinAlg::MultPoly(tempPoly,tempPolyEigenvalue,sizeTempPoly,2);
        sizeTempPoly++;

    }
    for(unsigned i = 0; i < sizeTempPoly ; ++i)
    {
        ret(1,i+1) = tempPoly[i].real();
    }
    return ret;

}
Example #15
0
void PlotHandler::plot<Type>::realTimeDataUpdate(LinAlg::Matrix<Type> X, LinAlg::Matrix<Type> Y)
{
    for(unsigned i = 1; i <= X.getNumberOfColumns(); i++)
    {
        this->realTimeDataUpdate(X(1,i),Y(1,i));
    }
}
PlotHandler::plot<Type>::plot(LinAlg::Matrix<Type> Y, plotProperties properties)
{
    LinAlg::Matrix<Type> X = LinAlg::LineVector<Type>(0,Y.getNumberOfColumns());
    this->properties = properties;
    this->properties.PlotFrame->hide();
    this->generalPlot(X,Y);
    this->properties.PlotFrame->show();
}
PlotHandler::plot<Type>::plot(LinAlg::Matrix<Type> Y, QWidget *PlotFrame)
{
    LinAlg::Matrix<Type> X = LinAlg::LineVector<Type>(0,Y.getNumberOfColumns()-1);
    this->properties.setPlotFrame(PlotFrame);
    this->properties.PlotFrame->hide();
    this->generalPlot(X,Y);
    this->properties.PlotFrame->show();
}
LinAlg::Matrix<float> SistemasLineares::GaussSeidel(LinAlg::Matrix<float> MatrizUni, unsigned MaxIterations)
{
    LinAlg::Matrix<float> MatrizRes(MaxIterations, MatrizUni.getNumberOfColumns());

   //Deixa o vetor de chute inicial padronizado como vetor linha
   if(this->X0.getNumberOfColumns() < this->X0.getNumberOfRows())
   {
       ~this->X0;
   }
   //Insere o chute inicial na Matriz resposta
   for(unsigned i = 1; i < MatrizRes.getNumberOfColumns(); i++){
       MatrizRes(1,i) = this->X0(1,i);
   }

   //Laço para contar as linhas da Matriz Resposta
   for(unsigned k = 2; k <= MaxIterations; k++)
   {
       //Laço para contar as colunas da MatrizRes e linhas da MatrizUni.
       for(unsigned i = 1; i < MatrizUni.getNumberOfColumns(); i++)
       {
           float aux = 0;

           //Verificação das variáveis atualizadas (mesma linha)
           for(unsigned j = 1; j < i; j++)
           {
               aux += (MatrizUni(i,j)*MatrizRes(k, j));
           }

           //Verificação das variaveis não atualizadas (linha anterior)
           for(unsigned j = i+1; j < MatrizUni.getNumberOfColumns(); j++)
           {
               aux += (MatrizUni(i,j)*MatrizRes(k-1, j));
           }

           //Aplicação da formula geral (Bi-x1-x2)/Aii
           MatrizRes(k,i) = (MatrizUni(i, MatrizUni.getNumberOfColumns()) - aux)/MatrizUni(i,i);

           //Verifica se o valor de erro d(k) é o maior encontrado
           if(abs(MatrizRes(k,i) - MatrizRes(k-1,i)) > MatrizRes(k-1, MatrizRes.getNumberOfColumns()))
               MatrizRes(k-1, MatrizRes.getNumberOfColumns()) = abs(MatrizRes(k,i) - MatrizRes(k-1,i));
       }
   }
   return MatrizRes;
}
LinAlg::Matrix<Type> ModelHandler::NFIR<Type>::sim(LinAlg::Matrix<Type> Input)
{
    this->Input  = Input;
    LinAlg::Matrix<Type> TempOutput;

    for(unsigned i = 1; i <= Input.getNumberOfColumns(); ++i){
        this->setLinearVector(Input.GetColumn(i),TempOutput);
        TempOutput = TempOutput|~(this->LinearVectorA*this->ModelCoef);
    }
    this->Output = TempOutput;
    return this->Output;
}
Example #20
0
void restrictedOptimizationHandler::activeSet<Type>::KKT(LinAlg::Matrix<Type> A,
                                                         LinAlg::Matrix<Type> b,
                                                         LinAlg::Matrix<Type> &x,
                                                         LinAlg::Matrix<Type> &v)
{
    LinAlg::Matrix<Type> K = (this->QuadMat | (~A)) ||
                             (A | LinAlg::Zeros<Type>(A.getNumberOfRows(),A.getNumberOfRows()));
    LinAlg::Matrix<Type> L = -this->LinMat || b;
    LinAlg::Matrix<Type> S = (((~K)*K)^-1)*(~K)*L;
    x = S(from(1)-->this->LinMat.getNumberOfRows(),1);
    if(A.getNumberOfRows() > 0)
        v = S(from(this->LinMat.getNumberOfRows()+1)-->(this->LinMat.getNumberOfRows() + b.getNumberOfRows()),
              from(1)-->S.getNumberOfColumns());
}
Example #21
0
void LinAlg::Matrix<Type>::swap (const LinAlg::Matrix<OtherMatrixType>& otherMatrix)
{
    using std::swap;

    LinAlg::Matrix<Type> temp(otherMatrix.getNumberOfRows(), otherMatrix.getNumberOfColumns());

    for(unsigned i = 0; i < temp.rows; i++)
        for(unsigned j = 0; j < temp.columns; j++)
            temp.mat[i][j] = otherMatrix(i, j);

    swap (rows, temp.rows);
    swap (columns, temp.columns);

    swap (mat, temp.mat);
}
void LinAlg::QR_Factorization (const LinAlg::Matrix<Type>& input_matrix,
                               LinAlg::Matrix<Type>& output_Q_matrix,
                               LinAlg::Matrix<Type>& output_R_matrix)
{

    //Constants calculated and needed for the QR algorithm.
    unsigned R_rows = input_matrix.getNumberOfRows(), R_columns = input_matrix.getNumberOfColumns();
    Type tal, gama, sigma;

    output_Q_matrix = LinAlg::Eye<Type>(R_rows);
    output_R_matrix = input_matrix;

    for(unsigned j = 1; j <= R_columns; j++)
        for(unsigned i = R_rows; i >= j + 1; i--)
        {
            if(output_R_matrix(i, j) != 0)
            {
                LinAlg::Matrix<Type> temp;

                temp = LinAlg::Eye<Type>(R_rows);

                if(std::abs(output_R_matrix(i - 1, j)) > std::abs(output_R_matrix(i, j)))
                {
                    tal = output_R_matrix(i, j)/output_R_matrix(i - 1, j);
                    gama = 1/(std::sqrt(1 + std::pow(tal, 2)));
                    sigma = tal*gama;
                }
                else
                {
                    tal = output_R_matrix(i - 1, j)/output_R_matrix(i, j);
                    sigma = 1/(std::sqrt(1 + std::pow(tal, 2)));
                    gama = sigma*tal;
                }

                temp(i, i) = gama;
                temp(i, i - 1) = sigma;
                temp(i - 1, i) = -sigma;
                temp(i - 1, i - 1) = gama;

                output_R_matrix = (~temp)*output_R_matrix;
                output_Q_matrix *= temp;
            }

        }

}
Type NeuralNetworkHandler::Neuron<Type>::sim(LinAlg::Matrix<Type> Input, LinAlg::Matrix<Type> Weight, Type Bias){

    Type tempOutput = 0;

    if(Input.getNumberOfRows() < Input.getNumberOfColumns() || Weight.getNumberOfRows() < Weight.getNumberOfColumns())
        std::cout<<"As dimensoes nao batem. Impossivel realizar operacao."<<std::endl;
    else{
        this->Input = Input;
        this->Weight = Weight;
        this->Bias = Bias;

        tempOutput = ((~this->Input * this->Weight) - this->Bias)(1,1);
        //std::cout<<tempOutput<<std::endl;

        if(this->ActiveFunctionType == "Signal")
            this->Output = this->Signal(tempOutput);
        else if(this->ActiveFunctionType == "HardLimit")
            this->Output = this->HardLimit(tempOutput);
        else if(this->ActiveFunctionType == "Linear")
            this->Output = this->Linear(tempOutput);
        else if(this->ActiveFunctionType == "PositiveLinear")
            this->Output = this->PositiveLinear(tempOutput);
        else if(this->ActiveFunctionType == "SymmetricLinear")
            this->Output = this->SymmetricLinear(tempOutput);
        else if(this->ActiveFunctionType == "SaturatingLinear")
            this->Output = this->SaturatingLinear(tempOutput);
        else if(this->ActiveFunctionType == "Gaussian")
            this->Output = this->Gaussian(tempOutput);
        else if(this->ActiveFunctionType == "Multiquadrics")
            this->Output = this->Multiquadrics(tempOutput);
        else if(this->ActiveFunctionType == "InverseMultiquadrics")
            this->Output = this->InverseMultiquadrics(tempOutput);
        else if(this->ActiveFunctionType == "Sigmoid")
            this->Output = this->Sigmoid(tempOutput);
        else if(this->ActiveFunctionType == "HyperbolicTangentSigmoid")
            this->Output = this->HyperbolicTangentSigmoid(tempOutput);
        else{
            std::cout<<"Metodo nao encontrado"<<std::endl;
            this->Output = tempOutput;
        }
        return Output;
    }
    return 0;
}
void SistemasLineares::CritLinhas(LinAlg::Matrix<float> MatrizUni)
{
    LinAlg::Matrix<float> MatrizRes(1,MatrizUni.getNumberOfRows());
    for(unsigned i = 1; i <= MatrizUni.getNumberOfRows(); i++)
    {
        for(unsigned j =1; j <= MatrizUni.getNumberOfColumns(); j++)
        {
            if(i != j)
            {
                MatrizRes(1, i) += MatrizUni(i,j)/MatrizUni(i,i);
            }
        }
        if(MatrizRes(1, i) > 1)
        {
            cout<<"O sistema não possui solução para qualquer valor inicial de X0";
            break;
        }
    }
}
Example #25
0
bool LinAlg::Matrix<Type>::CheckDimensions (const LinAlg::Matrix<OtherMatrixType>& rhs, unsigned operation)
{
    bool checked;

    switch(operation)
    {
    case 0:
        try
        {
            if((this->rows == rhs.getNumberOfRows()) && (this->columns == rhs.getNumberOfColumns()))
                checked = true;
            else
            {
                throw "As dimensoes nao batem. Impossivel realizar operacao";
                checked = false;
            }
        }
        catch(const char* msg)
        {
            std::cerr << msg;
        }
        break;
    case 1:
        try
        {
            if(this->columns == rhs.getNumberOfRows())
                checked = true;
            else
            {
                throw "As dimensoes nao batem. Impossivel realizar operacao";
                checked = false;
            }
        }
        catch(const char* msg)
        {
            std::cerr << msg;
        }
        break;
    }

    return checked;
}
std::string PolynomHandler::printSmallPolynom(const LinAlg::Matrix<Type> &rhs, const char &variable)
{
    std::ostringstream ret;
    const char plusSignal  = '+';
    const char minusSignal = '-';
    unsigned Size = rhs.getNumberOfColumns();

    if(Size == 0)
        return ret.str();

    for(unsigned i = 1; i <= Size; ++i)
    {
        if(i != 1 && rhs(1,i) > 0)
        {
            ret << plusSignal << ' ';
        }
        else if(rhs(1,i) < 0)
        {
            ret << minusSignal << ' ';
        }

        if(((rhs(1,i) != 1) && (rhs(1,i) > 0)) || ((i == Size) && (rhs(1,i) > 0)))
            ret << rhs(1,i);
        else if((rhs(1,i) != -1 && rhs(1,i) < 0)|| ((i == Size) && (rhs(1,i) < 0)))
            ret << -rhs(1,i);

        if(Size-i > 0 && rhs(1,i) != 0)
        {
            ret << variable;
            if(i != Size-1)
            {
                ret << '^';
                ret << Size-i;
            }
        }

        ret << ' ';
    }

    return ret.str();
}
Example #27
0
void restrictedOptimizationHandler::simplex<Type>::optimize()
{
    if(this->N.A.getNumberOfColumns() != 0 && this->N.A.getNumberOfRows() != 0)
    {
        this->P.A = (this->P.A | LinAlg::Eye<Type>(this->P.A.getNumberOfRows())                               | LinAlg::Zeros<Type>(this->N.A.getNumberOfRows(),this->N.A.getNumberOfRows())) ||
                  (this->N.A | LinAlg::Zeros<Type>(this->P.A.getNumberOfRows(),this->P.A.getNumberOfRows()) | -LinAlg::Eye<Type>(this->N.A.getNumberOfRows()));
//        std::cout << this->A << "\n";
        this->P.b = this->P.b || this->N.b;
        this->A = this->P.A   | LinAlg::Eye<Type>(this->P.A.getNumberOfRows())||
                  this->E.A | LinAlg::Zeros<Type>(this->E.A.getNumberOfRows(),this->P.A.getNumberOfRows());
        this->b = this->P.b || this->E.b;
//        LinAlg::Matrix<Type> c = this->function2Optimize || LinAlg::Zeros<Type>(1,this->P.b.getNumberOfRows() - this->E.b.getNumberOfRows() + this->N.b.getNumberOfRows());
        LinAlg::Matrix<Type> w = LinAlg::Zeros<Type>(this->A.getNumberOfColumns() - this->P.A.getNumberOfRows(),1)||
                                 LinAlg::Ones<Type>(this->P.A.getNumberOfRows(),1);
        LinAlg::Matrix<Type> B = LinAlg::LineVector<Type>(this->A.getNumberOfColumns() - this->P.A.getNumberOfRows() +1, this->A.getNumberOfColumns());
        LinAlg::Matrix<Type> n = LinAlg::LineVector<Type>(1,this->A.getNumberOfColumns() - this->P.A.getNumberOfRows());
        simplexOptimization(w,this->A,this->b,B,n);
        this->x = this->x(1,from(1)-->this->x.getNumberOfRows()-this->P.A.getNumberOfRows());
        this->A = this->A(from(1)-->this->A.getNumberOfRows(),from(1)-->this->A.getNumberOfColumns()-this->P.A.getNumberOfRows());
        LinAlg::Matrix<Type> c = this->function2Optimize ||
                                 LinAlg::Zeros<Type>(this->b.getNumberOfRows(),1);
        n = n(1,from(1)-->n.getNumberOfColumns()-this->P.A.getNumberOfRows());
        simplexOptimization(c,this->A,this->b,B,n);
        this->set = B;
    }
    else
    {
        this->A = (this->P.A | LinAlg::Eye<Type>(this->P.A.getNumberOfRows()))||
                  (this->E.A | LinAlg::Zeros<Type>(this->E.A.getNumberOfRows(),this->P.A.getNumberOfRows()));
        this->b = this->P.b || this->E.b;

        LinAlg::Matrix<Type> c = this->function2Optimize ||
                                 LinAlg::Zeros<Type>(this->b.getNumberOfColumns(), 1);

        LinAlg::Matrix<Type> B = LinAlg::LineVector<Type>(this->A.getNumberOfColumns() - this->P.A.getNumberOfRows() +1, this->A.getNumberOfColumns(),1);
        LinAlg::Matrix<Type> n = LinAlg::LineVector<Type>(1,this->A.getNumberOfColumns() - this->P.A.getNumberOfRows());
        simplexOptimization(c,this->A,this->b,B,n);
        this->set = B;
    }
//    std::cout << this->x << "\n\n";
}
void PolynomHandler::Polynom<Type>::init(LinAlg::Matrix<Type> Num, LinAlg::Matrix<Type> Den)

{
    using namespace std;

    this->sizeNum = Num.getNumberOfColumns();
    this->num = initPointer<Type>(Num.getNumberOfColumns());
    for (unsigned i = 0; i < Num.getNumberOfColumns(); ++i)
        this->num[i] = (Type) Num(1, i+1);

    this->sizeDen = Den.getNumberOfColumns();
    this->den = initPointer<Type>(Den.getNumberOfColumns());
    for (unsigned i = 0; i < Den.getNumberOfColumns(); ++i)
        this->den[i] = (Type) Den(1 , i+1);
    this->x = 'x';
}
bool LinAlg::Matrix<Type>::CheckDimensions (const LinAlg::Matrix<OtherMatrixType>& rhs, unsigned operation)
{
    bool checked = false;

    switch(operation)
    {
    case 0:
        if((this->rows == rhs.getNumberOfRows()) && (this->columns == rhs.getNumberOfColumns()))
            checked = true;
        else
            std::cout << "As dimensoes nao batem. Impossivel realizar operacao." << std::endl;
        break;
    case 1:
        if(this->columns == rhs.getNumberOfRows())
            checked = true;
        else
            std::cout << "As dimensoes nao batem. Impossivel realizar operacao." << std::endl;
        break;
    }

    return checked;
}
Example #30
0
LinAlg::Matrix<Type> restrictedOptimizationHandler::simplex<Type>::EscalSimplex(LinAlg::Matrix<Type> A,
                                                                                unsigned index)
{
    for(unsigned i = 1; i <= A.getNumberOfColumns(); ++i)
        A(index,i) = A(index,i)/A(index,A.getNumberOfColumns());

    for (unsigned i = 1; i <= A.getNumberOfRows(); ++i)
        if(i != index)
        {
            Type m = A(i,A.getNumberOfColumns())/A(index,A.getNumberOfColumns());
            for(unsigned j = 1; j <= A.getNumberOfColumns(); ++j)
                A(i,j)=A(i,j)-m*A(index,j);
        }

//    Xb   = A(from(1)-->A.getNumberOfRows(),A.getNumberOfColumns()-1);
    LinAlg::Matrix<Type> Binv = A(from(1)-->A.getNumberOfRows(),from(1)-->A.getNumberOfColumns()-2);
//    X    = A(from(1)--> A.getNumberOfRows(),A.getNumberOfColumns());
    return Binv;
}