Exemplo n.º 1
0
static void ReflectMat
   (enum CBLAS_ORDER Order, enum CBLAS_UPLO Uplo, int N, TYPE *A, int lda)
/*
 * Takes a symmetric matrix, and makes it general by reflecting across diagonal
 */
{
   const int lda2 = (lda SHIFT);
   int j;

   if (Order == CblasRowMajor)
   {
      if (Uplo == CblasUpper) Uplo = CblasLower;
      else Uplo = CblasUpper;
   }
   if (Uplo == CblasUpper)
   {
      for (j=0; j < N; j++) cblas_copy(j, A+j*lda2, 1, A+(j SHIFT), lda);
   }
   else /* lower matrix */
   {
      for (j=0; j < N; j++) cblas_copy(j, A+(j SHIFT), lda,  A+j*lda2, 1);
   }
}
Exemplo n.º 2
0
static void ReflectHE
   (enum CBLAS_ORDER Order, enum CBLAS_UPLO Uplo, int N, TYPE *A, int lda)
/*
 * Reflects matrix, makes it hermitian
 */
{
   #ifdef TREAL
      ReflectMat(Order, Uplo, N, A, lda);
   #else
   const int lda2 = (lda SHIFT);
   int j;

   if (Order == CblasRowMajor)
   {
      if (Uplo == CblasUpper) Uplo = CblasLower;
      else Uplo = CblasUpper;
   }
   if (Uplo == CblasUpper)
   {
      for (j=0; j < N; j++)
      {
         cblas_copy(j, A+j*lda2, 1, A+(j SHIFT), lda);
         Mjoin(PATLU,scal)(j, ATL_rnone, A+(j SHIFT)+1, lda*2);
      }
   }
   else /* lower matrix */
   {
      for (j=0; j < N; j++)
      {
         cblas_copy(j, A+(j SHIFT), lda,  A+j*lda2, 1);
         Mjoin(PATLU,scal)(j, ATL_rnone, A+j*lda2+1, 2);
      }
   }
   Mjoin(PATLU,zero)(N, A+1, (lda+1)SHIFT);
   #endif
}
Exemplo n.º 3
0
static void ATL_L2GE
   (const enum CBLAS_ORDER Order, const int N, const TYPE *L, const int ldl,
    TYPE *C, const int ldc)
{
   int j;
   if (Order == CblasRowMajor) ATL_U2GE(CblasColMajor, N, L, ldl, C, ldc);
   else
   {
      for (j=0; j < N; j++)
      {
         if (j > 1) Mjoin(PATL,zero)(j-1, C+j*(N SHIFT), 1);
         cblas_copy(N-j, L+j*((ldl+1)SHIFT), 1, C+j*((N+1)SHIFT), 1);
      }
   }
}
Exemplo n.º 4
0
static void ATL_U2GE
   (const enum CBLAS_ORDER Order, const int N, const TYPE *U, const int ldu,
    TYPE *C, const int ldc)
{
   int j;

   if (Order == CblasRowMajor) ATL_L2GE(CblasColMajor, N, U, ldu, C, ldc);
   else
   {
      for (j=0; j < N; j++)
      {
         cblas_copy(j+1, U+j*(ldu SHIFT), 1, C+j*(N SHIFT), 1);
         if (j != N-1) Mjoin(PATL,zero)(N-j-1, C+((1+j*(N+1))SHIFT), 1);
      }
   }
}
Exemplo n.º 5
0
template <typename IndexType, typename ValueType> void proj_to_lp_cpu(csr_matrix<IndexType, ValueType> &inputSet,
        ValueType * minNormVector, IndexType inSetDim, IndexType vectorDim, ValueType tollerance) {
    ValueType lpShift = (ValueType) 10; //Установим начальное смещение, оно должно быть очень большим
    //переместим наш политоп в соответствии с этим смещением, это будет означать
    //что мы переместили начало координат в эту точку, что соответствует тому что мы отнимим
    //от вектора b из уравнения Ax <=b следующую величину b + t * A * c (с = [1])
    ValueType* vshift = (ValueType *) malloc(inSetDim * sizeof (ValueType));
    ValueType* t_C = (ValueType *) malloc((vectorDim - 1) * sizeof (ValueType));
    for (IndexType i = 0; i < vectorDim - 1; i++) {
        t_C[i] = -lpShift;
    }

    IndexType maxApprIters = 10;
    ValueType *x_s = new_host_array<ValueType > (vectorDim);
    ValueType *delta_x = new_host_array<ValueType > (vectorDim);
    ValueType* v_t = new_host_array<ValueType > (vectorDim); //- 1); //(ValueType *) malloc((vectorDim -1) * sizeof (ValueType));

    IndexType *kvec = new_host_array<IndexType>(2 * inputSet.num_cols + 1 );
    IndexType *kvec_old = new_host_array<IndexType>(2 * inputSet.num_cols + 1 );
    IndexType *basisVecInx = new_host_array<IndexType>(2 * inputSet.num_cols + 1);
    IndexType baselen = 0;

    for (IndexType i = 0; i < 2 * inputSet.num_cols + 1; i++) {
        kvec[i] = 0;
        kvec_old[i] = 0;
    }

    //Начальный x_s это ноль
    cblas_scal(vectorDim, 0.0, x_s, 1);
    for (IndexType apprIt = 0; apprIt < maxApprIters; apprIt++) {
        //Получаем новое B
        cblas_copy(vectorDim - 1, t_C, 1, v_t, 1);
        cblas_axpy(vectorDim - 1, -1.0, x_s, 1, v_t, 1);
        //std::cout << "Vector V_t\n";
        //printMatrixCPU(vectorDim - 1, 1, v_t);
        cblas_copy(vectorDim, x_s, 1, delta_x, 1);
        inputSet.num_rows--;
        spmv_csr_t_serial_host(inputSet, v_t, vshift);
        inputSet.num_rows++;

        //printMatrixCPU(inSetDim, 1, vshift);
        incValsInLstMtxRowVec(inputSet, inputSet.num_rows - 1, vshift);

        std::cout << "Input set on iteration\n";
        //print_csr_matrix(inputSet);
        //getProjectionOnConus(inputSet, minNormVector, inSetDim, vectorDim, tollerance);
        csr_matrix<IndexType, ValueType> inSetCopy = getCsrMtxCopy(inputSet);

        if(apprIt > 0){
            memcpy(kvec_old, kvec, (2 * inputSet.num_cols + 1) * sizeof (IndexType));
        }
        //print_csr_matrix(inSetCopy);
        //projOnFixedSimplex(inSetCopy, minNormVector, kvec, basisVecInx, baselen, tollerance );
        getMinNormElemOutRepr(in_a_csc, minNormVector, 0.0001, kvec,  basisVecInx, baselen, numberOfEqConstr);
        delete_host_matrix(inSetCopy);

        IndexType kvec_razn = 0;
        if(apprIt > 0){
            for (IndexType i = 0; i < 2 * inputSet.num_cols + 1; i++) {
                if(kvec[i] == kvec_old[i] && kvec[i] == 1){
                    kvec_razn++;
                }
            }
        }
        std::cout << "KVEC RAZN is = " << kvec_razn <<"\n";
        std::cout << "baselen is = " << baselen <<"\n";

        cblas_axpy(vectorDim, -1.0, v_t, 1, minNormVector, 1);
        //std::cout << "Min norm vector on iteration\n";
        //printMatrixCPU(vectorDim, 1, minNormVector);

        decValsInLstMtxRowVec(inputSet, inputSet.num_rows - 1, vshift);


        cblas_copy(vectorDim, minNormVector, 1, x_s, 1);

        cblas_axpy(vectorDim, -1.0, x_s, 1, delta_x, 1);
        ValueType z_summ = 0.0;        
        for (IndexType i = 0; i < vectorDim - 1; i++) {
            z_summ += minNormVector[i];
        }
        std::cout << "Summ of elements " << z_summ << " on "<< apprIt <<" iteration\n";
        ValueType dist = cblas_dot(vectorDim - 1, delta_x, 1, delta_x, 1);
        if (dist < tollerance * tollerance) {
            std::cout << "iterations count :" << apprIt << "\n";
            break;
        }       
    }

    //cblas_axpy(vectorDim, -1.0, v_t, 1, minNormVector, 1);
    ValueType dist = cblas_dot(vectorDim - 1, minNormVector, 1, minNormVector, 1);
    std::cout << "Min Vector Lengh = " << sqrt(dist) << "\n";
    ValueType z_summ = 0.0;
    IndexType nonzer_summ = 0;
    for (IndexType i = 0; i < vectorDim - 1; i++) {
        z_summ += minNormVector[i];
        if (minNormVector[i] != 0.0) {
            nonzer_summ++;
        }
    }

    std::cout << "Summ of elements " << z_summ << " \n";
    std::cout << "Count of nonzerros " << nonzer_summ << " \n";
    //printMatrixToFileForOctave(vectorDim, 1, minNormVector);
    ValueType* b_contr = new_host_array<ValueType > (inSetDim);
    inputSet.num_rows--;
    spmv_csr_t_serial_host(inputSet, minNormVector, b_contr);
    inputSet.num_rows++;
    //printMatrixCPU(inSetDim, 1, b_contr);
    IndexType b_begin = inputSet.Ap[vectorDim - 1];
    IndexType b_end = inputSet.Ap[vectorDim];
    IndexType inconsCount = 0;
    for (IndexType i = b_begin; i < b_end; i++) {
        IndexType j = inputSet.Aj[i];
        if (b_contr[j] > inputSet.Ax[i] + tollerance * tollerance) {
            //std::cout << "b_contr " << b_contr[j] << " Ax " << inputSet.Ax[i] << " \n";
            ValueType razn = b_contr[j] -  inputSet.Ax[i];
            //printf("bcontr[%i] %e Ax %e  razn %e\n", j ,b_contr[j], inputSet.Ax[i], razn);
            inconsCount++;
        }
    }
    std::cout << "Inconsistent X count: " << inconsCount << " \n";

}