Esempio n. 1
0
int main(void)
{
    int i, j;

    transposeMatrix();

    for (i = 0; i < 4; i++)
    {
        for (j = 0; j < 5; j++)
            printf("%4i", gValues[i][j]);
        printf("\n");
    }
    printf("Transposed = \n");
    for (i = 0; i < 5; i++)
    {
        for (j = 0; j < 4; j++)
            printf("%4i", gResult[i][j]);
        printf("\n");
    }

    return 0;
}
Esempio n. 2
0
gsl_matrix* reconstructData (gsl_matrix* t_data, gsl_matrix* rot, gsl_vector* means) {

    // rec_data = rot * t_data
    gsl_matrix* rec_data_t = gsl_matrix_alloc(rot->size1, t_data->size2);
    gsl_blas_dgemm( CblasNoTrans,
                    CblasNoTrans,
                    1.0, rot, t_data, 0.0, rec_data_t);


    gsl_matrix* rec_data = gsl_matrix_alloc(rec_data_t->size2, rec_data_t->size1);
    transposeMatrix(rec_data_t, rec_data);

    gsl_matrix_free(rec_data_t);

    // rec_data + means
    for (unsigned int j = 0; j < (rec_data->size2); j++) {
        gsl_vector_view vv = gsl_matrix_column(rec_data,j);
        gsl_vector* v = &vv.vector;
        gsl_vector_add_constant(v, gsl_vector_get(means,j));
    }

    return(rec_data);
}
Esempio n. 3
0
void biconjugateGradientMethod(double** matrix, double* rightPart, double* outVector, int number, double precision, int maxIteration, bool periodic, int rank, int nprocs) {
	printf("start biconjugate gradient\n");
	double* residual = new double[number];
	double* prevResidual = new double[number];
	double* z = new double[number];
	double* p = new double[number];
	double* s = new double[number];
	double* tempVector = new double[number];
	double* tempVector2 = new double[number];
	double** transposedMatrix = new double*[number];

	for(int i = 0; i < number; ++i){
		transposedMatrix[i] = new double[number];
		outVector[i] = 0;
		prevResidual[i] = rightPart[i];
		z[i]= rightPart[i];
		p[i] = rightPart[i];
		s[i] = rightPart[i];
		tempVector[i] = 0;
		tempVector2[i] = 0;
	}

	transposeMatrix(transposedMatrix, matrix, number);



	int iteration = 0;


    double prevResidualNorm2 = scalarMultiplyLargeVectors(p, prevResidual, number, periodic, rank, nprocs);
    double residualNorm2 = prevResidualNorm2;
	double rightPartNorm2 = scalarMultiplyLargeVectors(rightPart, rightPart, number, periodic, rank, nprocs);

	double relativeError = sqrt(residualNorm2/rightPartNorm2);

	while((iteration < maxIteration) && (iteration < number) && (relativeError > (precision/number))){
		printf("biconjugate gradient iteration %d\n", iteration);

		multiplyMatrixVector(tempVector, matrix, z, number, periodic, rank, nprocs);
		multiplyMatrixVector(tempVector2, transposedMatrix, s, number, periodic, rank, nprocs);

		double alpha = prevResidualNorm2/scalarMultiplyLargeVectors(tempVector, s, number, periodic, rank, nprocs);

		for(int i = 0; i < number; ++i){
						outVector[i] += alpha*z[i];
						residual[i] = prevResidual[i] - alpha*tempVector[i];
						p[i] = p[i] - alpha*tempVector2[i];
		}

        residualNorm2 = scalarMultiplyLargeVectors(p, residual, number, periodic, rank, nprocs);

		double beta  = residualNorm2/prevResidualNorm2;

		for(int i = 0; i < number; ++i){
						z[i] = residual[i] + beta*z[i];
						s[i] = p[i] + beta*s[i];
		}

		prevResidualNorm2 = residualNorm2;

		relativeError = sqrt(scalarMultiplyLargeVectors(residual, residual, number, periodic, rank, nprocs)/rightPartNorm2);
		iteration++;
	}

	for(int i = 0; i < number; ++i){
		delete[] transposedMatrix[i];
	}
	delete[] residual;
	delete[] prevResidual;
	delete[] z;
	delete[] p;
	delete[] s;
	delete[] tempVector;
	delete[] tempVector2;
	delete[] transposedMatrix;
}
// Render a frame
void AtmosphereSample::Render()
{
    float4x4 mViewProj = m_mCameraView * m_mCameraProj;

    LightAttribs LightAttrs;
    LightAttrs.f4DirOnLight = -m_f3LightDir;
    LightAttrs.f4DirOnLight.w = 0;

    float4 f4ExtraterrestrialSunColor = float4(10,10,10,10);
    LightAttrs.f4ExtraterrestrialSunColor = f4ExtraterrestrialSunColor;// *m_fScatteringScale;
    LightAttrs.f4AmbientLight = float4( 0, 0, 0, 0 );


    // m_iFirstCascade must be initialized before calling RenderShadowMap()!
    m_PPAttribs.m_iFirstCascade = std::min(m_PPAttribs.m_iFirstCascade, m_TerrainRenderParams.m_iNumShadowCascades - 1);
    m_PPAttribs.m_fFirstCascade = (float)m_PPAttribs.m_iFirstCascade;

	RenderShadowMap(m_pImmediateContext, LightAttrs, m_mCameraView, m_mCameraProj);
    
    LightAttrs.ShadowAttribs.bVisualizeCascades = m_bVisualizeCascades ? TRUE : FALSE;

    // Calculate location of the sun on the screen
    float4 &f4LightPosPS = LightAttrs.f4LightScreenPos;
    f4LightPosPS = LightAttrs.f4DirOnLight * mViewProj;
    f4LightPosPS.x /= f4LightPosPS.w;
    f4LightPosPS.y /= f4LightPosPS.w;
    f4LightPosPS.z /= f4LightPosPS.w;
    float fDistToLightOnScreen = length( (float2&)f4LightPosPS );
    float fMaxDist = 100;

    if( fDistToLightOnScreen > fMaxDist )
        (float2&)f4LightPosPS *= fMaxDist/fDistToLightOnScreen;

    const auto& SCDesc = m_pSwapChain->GetDesc();
    // Note that in fact the outermost visible screen pixels do not lie exactly on the boundary (+1 or -1), but are biased by
    // 0.5 screen pixel size inwards. Using these adjusted boundaries improves precision and results in
    // smaller number of pixels which require inscattering correction
    LightAttrs.bIsLightOnScreen = fabs(f4LightPosPS.x) <= 1.f - 1.f/(float)SCDesc.Width && 
                                    fabs(f4LightPosPS.y) <= 1.f - 1.f/(float)SCDesc.Height;

    {
        MapHelper<LightAttribs> pLightAttribs( m_pImmediateContext, m_pcbLightAttribs, MAP_WRITE, MAP_FLAG_DISCARD );
        *pLightAttribs = LightAttrs;
    }

    // The first time GetAmbientSkyLightSRV() is called, the ambient sky light texture 
    // is computed and render target is set. So we need to query the texture before setting 
    // render targets
    auto *pAmbientSkyLightSRV = m_pLightSctrPP->GetAmbientSkyLightSRV(m_pDevice, m_pImmediateContext);

    m_pImmediateContext->SetRenderTargets( 0, nullptr, nullptr );

    const float ClearColor[] = {  0.350f,  0.350f,  0.350f, 1.0f }; 
    const float Zero[] = {  0.f,  0.f,  0.f, 0.f };
    m_pImmediateContext->ClearRenderTarget(nullptr, m_bEnableLightScattering ? Zero : ClearColor);

    ITextureView *pRTV = nullptr, *pDSV = nullptr;
    if( m_bEnableLightScattering )
    {
        pRTV = m_pOffscreenColorBuffer->GetDefaultView( TEXTURE_VIEW_RENDER_TARGET );
        pDSV = m_pOffscreenDepthBuffer->GetDefaultView( TEXTURE_VIEW_DEPTH_STENCIL );
        m_pImmediateContext->SetRenderTargets( 1, &pRTV, pDSV );
        m_pImmediateContext->ClearRenderTarget(pRTV, Zero);
    }
    else
    {
        pRTV = nullptr;
        pDSV = nullptr;
        m_pImmediateContext->SetRenderTargets( 0, nullptr, nullptr );
    }
        
    m_pImmediateContext->ClearDepthStencil(pDSV, CLEAR_DEPTH_FLAG, 1.f);

    {
        MapHelper<CameraAttribs> CamAttribs( m_pImmediateContext, m_pcbCameraAttribs, MAP_WRITE, MAP_FLAG_DISCARD );
        CamAttribs->mViewProjT = transposeMatrix( mViewProj );
        CamAttribs->mProjT = transposeMatrix( m_mCameraProj );
        CamAttribs->mViewProjInvT = transposeMatrix( inverseMatrix(mViewProj) );
        float fNearPlane = 0.f, fFarPlane = 0.f;
        GetNearFarPlaneFromProjMatrix( m_mCameraProj, fNearPlane, fFarPlane, m_bIsDXDevice);
        CamAttribs->fNearPlaneZ = fNearPlane;
        CamAttribs->fFarPlaneZ = fFarPlane * 0.999999f;
        CamAttribs->f4CameraPos = m_f3CameraPos;
    }    

    // Render terrain
    auto *pPrecomputedNetDensitySRV = m_pLightSctrPP->GetPrecomputedNetDensitySRV();
    m_TerrainRenderParams.DstRTVFormat = m_bEnableLightScattering ? m_pOffscreenColorBuffer->GetDesc().Format : m_pSwapChain->GetDesc().ColorBufferFormat;
    m_EarthHemisphere.Render( m_pImmediateContext, 
                              m_TerrainRenderParams, 
                              m_f3CameraPos, 
                              mViewProj, 
                              m_pShadowMapSRV, 
                              pPrecomputedNetDensitySRV, 
                              pAmbientSkyLightSRV, 
                              false);
	
    if( m_bEnableLightScattering )
    {
        FrameAttribs FrameAttribs;

        FrameAttribs.pDevice = m_pDevice;
        FrameAttribs.pDeviceContext = m_pImmediateContext;
        FrameAttribs.dElapsedTime = m_fElapsedTime;
        FrameAttribs.pLightAttribs = &LightAttrs;

        m_PPAttribs.m_iNumCascades = m_TerrainRenderParams.m_iNumShadowCascades;
        m_PPAttribs.m_fNumCascades = (float)m_TerrainRenderParams.m_iNumShadowCascades;

        FrameAttribs.pcbLightAttribs = m_pcbLightAttribs;
        FrameAttribs.pcbCameraAttribs = m_pcbCameraAttribs;

        m_PPAttribs.m_fMaxShadowMapStep = static_cast<float>(m_uiShadowMapResolution / 4);

        m_PPAttribs.m_f2ShadowMapTexelSize = float2( 1.f / static_cast<float>(m_uiShadowMapResolution), 1.f / static_cast<float>(m_uiShadowMapResolution) );
        m_PPAttribs.m_uiShadowMapResolution = m_uiShadowMapResolution;
        // During the ray marching, on each step we move by the texel size in either horz 
        // or vert direction. So resolution of min/max mipmap should be the same as the 
        // resolution of the original shadow map
        m_PPAttribs.m_uiMinMaxShadowMapResolution = m_uiShadowMapResolution;
        m_PPAttribs.m_uiInitialSampleStepInSlice = std::min( m_PPAttribs.m_uiInitialSampleStepInSlice, m_PPAttribs.m_uiMaxSamplesInSlice );
        m_PPAttribs.m_uiEpipoleSamplingDensityFactor = std::min( m_PPAttribs.m_uiEpipoleSamplingDensityFactor, m_PPAttribs.m_uiInitialSampleStepInSlice );

        FrameAttribs.ptex2DSrcColorBufferSRV = m_pOffscreenColorBuffer->GetDefaultView(TEXTURE_VIEW_SHADER_RESOURCE);
        FrameAttribs.ptex2DSrcColorBufferRTV = m_pOffscreenColorBuffer->GetDefaultView(TEXTURE_VIEW_RENDER_TARGET);
        FrameAttribs.ptex2DSrcDepthBufferSRV = m_pOffscreenDepthBuffer->GetDefaultView(TEXTURE_VIEW_SHADER_RESOURCE);
        FrameAttribs.ptex2DSrcDepthBufferDSV = m_pOffscreenDepthBuffer->GetDefaultView(TEXTURE_VIEW_DEPTH_STENCIL);
        FrameAttribs.ptex2DShadowMapSRV      = m_pShadowMapSRV;
        FrameAttribs.pDstRTV                 = 0;// mpBackBufferRTV;

        // Then perform the post processing, swapping the inverseworld view  projection matrix axes.
        m_pLightSctrPP->PerformPostProcessing(FrameAttribs, m_PPAttribs);
    }
}
void AtmosphereSample::RenderShadowMap(IDeviceContext *pContext,
                                        LightAttribs &LightAttribs, 
                                        const float4x4 &mCameraView, 
                                        const float4x4 &mCameraProj )
{
    ShadowMapAttribs& ShadowMapAttribs = LightAttribs.ShadowAttribs;

    float3 v3DirOnLight = (float3&)LightAttribs.f4DirOnLight;
    float3 v3LightDirection = -v3DirOnLight;

    // Declare working vectors
    float3 vLightSpaceX, vLightSpaceY, vLightSpaceZ;

    // Compute an inverse vector for the direction on the sun
    vLightSpaceZ = v3LightDirection;
    // And a vector for X light space
    vLightSpaceX = float3( 1.0f, 0.0, 0.0 );
    // Compute the cross products
    vLightSpaceY = cross(vLightSpaceX, vLightSpaceZ);
    vLightSpaceX = cross(vLightSpaceZ, vLightSpaceY);
    // And then normalize them
    vLightSpaceX = normalize( vLightSpaceX );
    vLightSpaceY = normalize( vLightSpaceY );
    vLightSpaceZ = normalize( vLightSpaceZ );

    // Declare a world to light space transformation matrix
    // Initialize to an identity matrix
    float4x4 WorldToLightViewSpaceMatr =
        ViewMatrixFromBasis( vLightSpaceX, vLightSpaceY, vLightSpaceZ );

    ShadowMapAttribs.mWorldToLightViewT = transposeMatrix( WorldToLightViewSpaceMatr );

    float3 f3CameraPosInLightSpace = m_f3CameraPos * WorldToLightViewSpaceMatr;

    float fMainCamNearPlane, fMainCamFarPlane;
    GetNearFarPlaneFromProjMatrix( mCameraProj, fMainCamNearPlane, fMainCamFarPlane, m_bIsDXDevice);

    for(int i=0; i < MAX_CASCADES; ++i)
        ShadowMapAttribs.fCascadeCamSpaceZEnd[i] = +FLT_MAX;

    // Render cascades
    for(int iCascade = 0; iCascade < m_TerrainRenderParams.m_iNumShadowCascades; ++iCascade)
    {
        auto &CurrCascade = ShadowMapAttribs.Cascades[iCascade];
        float4x4 CascadeFrustumProjMatrix;
        float &fCascadeFarZ = ShadowMapAttribs.fCascadeCamSpaceZEnd[iCascade];
        float fCascadeNearZ = (iCascade == 0) ? fMainCamNearPlane : ShadowMapAttribs.fCascadeCamSpaceZEnd[iCascade-1];
        fCascadeFarZ = fMainCamFarPlane;

        if (iCascade < m_TerrainRenderParams.m_iNumShadowCascades-1) 
        {
            float ratio = fMainCamFarPlane / fMainCamNearPlane;
            float power = (float)(iCascade+1) / (float)m_TerrainRenderParams.m_iNumShadowCascades;
            float logZ = fMainCamNearPlane * pow(ratio, power);
        
            float range = fMainCamFarPlane - fMainCamNearPlane;
            float uniformZ = fMainCamNearPlane + range * power;

            fCascadeFarZ = m_fCascadePartitioningFactor * (logZ - uniformZ) + uniformZ;
        }

        float fMaxLightShaftsDist = 3e+5f;
        // Ray marching always starts at the camera position, not at the near plane.
        // So we must make sure that the first cascade used for ray marching covers the camera position
        CurrCascade.f4StartEndZ.x = (iCascade == m_PPAttribs.m_iFirstCascade) ? 0 : std::min(fCascadeNearZ, fMaxLightShaftsDist);
        CurrCascade.f4StartEndZ.y = std::min(fCascadeFarZ, fMaxLightShaftsDist);
        CascadeFrustumProjMatrix = mCameraProj;
        SetNearFarClipPlanes( CascadeFrustumProjMatrix, fCascadeNearZ, fCascadeFarZ, m_bIsDXDevice );

        float4x4 CascadeFrustumViewProjMatr = mCameraView * CascadeFrustumProjMatrix;
        float4x4 CascadeFrustumProjSpaceToWorldSpace = inverseMatrix(CascadeFrustumViewProjMatr);
        float4x4 CascadeFrustumProjSpaceToLightSpace = CascadeFrustumProjSpaceToWorldSpace * WorldToLightViewSpaceMatr;

        // Set reference minimums and maximums for each coordinate
        float3 f3MinXYZ(f3CameraPosInLightSpace), f3MaxXYZ(f3CameraPosInLightSpace);
        
        // First cascade used for ray marching must contain camera within it
        if( iCascade != m_PPAttribs.m_iFirstCascade )
        {
            f3MinXYZ = float3(+FLT_MAX, +FLT_MAX, +FLT_MAX);
            f3MaxXYZ = float3(-FLT_MAX, -FLT_MAX, -FLT_MAX);
        }

        for(int iClipPlaneCorner=0; iClipPlaneCorner < 8; ++iClipPlaneCorner)
        {
            float3 f3PlaneCornerProjSpace( (iClipPlaneCorner & 0x01) ? +1.f : - 1.f, 
                                           (iClipPlaneCorner & 0x02) ? +1.f : - 1.f,
                                            // Since we use complimentary depth buffering, 
                                            // far plane has depth 0
                                           (iClipPlaneCorner & 0x04) ? 1.f : (m_bIsDXDevice ? 0.f : -1.f));
            float3 f3PlaneCornerLightSpace = f3PlaneCornerProjSpace * CascadeFrustumProjSpaceToLightSpace;
            f3MinXYZ = min(f3MinXYZ, f3PlaneCornerLightSpace);
            f3MaxXYZ = max(f3MaxXYZ, f3PlaneCornerLightSpace);
        }

        // It is necessary to ensure that shadow-casting patches, which are not visible 
        // in the frustum, are still rendered into the shadow map
        f3MinXYZ.z -= AirScatteringAttribs().fEarthRadius * sqrt(2.f);
        
        // Align cascade extent to the closest power of two
        float fShadowMapDim = (float)m_uiShadowMapResolution;
        float fCascadeXExt = (f3MaxXYZ.x - f3MinXYZ.x) * (1 + 1.f/fShadowMapDim);
        float fCascadeYExt = (f3MaxXYZ.y - f3MinXYZ.y) * (1 + 1.f/fShadowMapDim);
        const float fExtStep = 2.f;
        fCascadeXExt = pow( fExtStep, ceil( log(fCascadeXExt)/log(fExtStep) ) );
        fCascadeYExt = pow( fExtStep, ceil( log(fCascadeYExt)/log(fExtStep) ) );
        // Align cascade center with the shadow map texels to alleviate temporal aliasing
        float fCascadeXCenter = (f3MaxXYZ.x + f3MinXYZ.x)/2.f;
        float fCascadeYCenter = (f3MaxXYZ.y + f3MinXYZ.y)/2.f;
        float fTexelXSize = fCascadeXExt / fShadowMapDim;
        float fTexelYSize = fCascadeXExt / fShadowMapDim;
        fCascadeXCenter = floor(fCascadeXCenter/fTexelXSize) * fTexelXSize;
        fCascadeYCenter = floor(fCascadeYCenter/fTexelYSize) * fTexelYSize;
        // Compute new cascade min/max xy coords
        f3MaxXYZ.x = fCascadeXCenter + fCascadeXExt/2.f;
        f3MinXYZ.x = fCascadeXCenter - fCascadeXExt/2.f;
        f3MaxXYZ.y = fCascadeYCenter + fCascadeYExt/2.f;
        f3MinXYZ.y = fCascadeYCenter - fCascadeYExt/2.f;

        CurrCascade.f4LightSpaceScale.x =  2.f / (f3MaxXYZ.x - f3MinXYZ.x);
        CurrCascade.f4LightSpaceScale.y =  2.f / (f3MaxXYZ.y - f3MinXYZ.y);
        CurrCascade.f4LightSpaceScale.z =  
                    (m_bIsDXDevice ? 1.f : 2.f) / (f3MaxXYZ.z - f3MinXYZ.z);
        // Apply bias to shift the extent to [-1,1]x[-1,1]x[0,1] for DX or to [-1,1]x[-1,1]x[-1,1] for GL
        // Find bias such that f3MinXYZ -> (-1,-1,0) for DX or (-1,-1,-1) for GL
        CurrCascade.f4LightSpaceScaledBias.x = -f3MinXYZ.x * CurrCascade.f4LightSpaceScale.x - 1.f;
        CurrCascade.f4LightSpaceScaledBias.y = -f3MinXYZ.y * CurrCascade.f4LightSpaceScale.y - 1.f;
        CurrCascade.f4LightSpaceScaledBias.z = -f3MinXYZ.z * CurrCascade.f4LightSpaceScale.z + (m_bIsDXDevice ? 0.f : -1.f);

        float4x4 ScaleMatrix = scaleMatrix( CurrCascade.f4LightSpaceScale.x, CurrCascade.f4LightSpaceScale.y, CurrCascade.f4LightSpaceScale.z );
        float4x4 ScaledBiasMatrix = translationMatrix( CurrCascade.f4LightSpaceScaledBias.x, CurrCascade.f4LightSpaceScaledBias.y, CurrCascade.f4LightSpaceScaledBias.z ) ;

        // Note: bias is applied after scaling!
        float4x4 CascadeProjMatr = ScaleMatrix * ScaledBiasMatrix;
        //D3DXMatrixOrthoOffCenterLH( &m_LightOrthoMatrix, MinX, MaxX, MinY, MaxY, MaxZ, MinZ);

        // Adjust the world to light space transformation matrix
        float4x4 WorldToLightProjSpaceMatr = WorldToLightViewSpaceMatr * CascadeProjMatr;
        float4x4 ProjToUVScale, ProjToUVBias;
        if( m_bIsDXDevice )
        {
            ProjToUVScale = scaleMatrix( 0.5f, -0.5f, 1.f );
            ProjToUVBias = translationMatrix( 0.5f, 0.5f, 0.f );
        }
        else
        {
            ProjToUVScale = scaleMatrix( 0.5f, 0.5f, 0.5f );
            ProjToUVBias = translationMatrix( 0.5f, 0.5f, 0.5f );
        }

        float4x4 WorldToShadowMapUVDepthMatr = WorldToLightProjSpaceMatr * ProjToUVScale * ProjToUVBias;
        ShadowMapAttribs.mWorldToShadowMapUVDepthT[iCascade] = transposeMatrix( WorldToShadowMapUVDepthMatr );

        m_pImmediateContext->SetRenderTargets( 0, nullptr, m_pShadowMapDSVs[iCascade] );
        m_pImmediateContext->ClearDepthStencil( m_pShadowMapDSVs[iCascade], CLEAR_DEPTH_FLAG, 1.f );

        // Render terrain to shadow map
        {
            MapHelper<CameraAttribs> CamAttribs( m_pImmediateContext, m_pcbCameraAttribs, MAP_WRITE, MAP_FLAG_DISCARD );
            CamAttribs->mViewProjT = transposeMatrix( WorldToLightProjSpaceMatr );
        }

        m_EarthHemisphere.Render(m_pImmediateContext, m_TerrainRenderParams, m_f3CameraPos, WorldToLightProjSpaceMatr, nullptr, nullptr, nullptr, true);
    }

    pContext->SetRenderTargets( 0, nullptr, nullptr );
}
Esempio n. 6
0
int main(int argc, char *argv[]) {
  int size, rank;
  int tag = 1234;
  MPI_Status status;

  const int rows = 4;
  const int columns = 2;
  int matrix[rows][columns];

  const int tRows = columns;
  const int tColumns = rows;

  // Init
  MPI_Init(&argc, &argv);
  MPI_Comm_size(MPI_COMM_WORLD, &size);
  MPI_Comm_rank(MPI_COMM_WORLD, &rank);

  if(size != 3) {
    printf("Su potrebne prave 3 procesy\n");
    return 0;
  }

  if(rows % 2 != 0) {
    printf("Matica musi mat parny pocet riadkov\n");
    return 0;
  }

  if(rank == 0) {
    // Generate matrix
    populateMatrix(rows, columns, matrix);

    // Print matrix
    printMatrix(rows, columns, matrix);

    int firstHalf[rows / 2][columns];
    int secondHalf[rows / 2][columns];
    divideMatrix(rows, columns, matrix, firstHalf, secondHalf);

    MPI_Send(firstHalf, (rows / 2) * columns, MPI_INT, 1, tag, MPI_COMM_WORLD);
    MPI_Send(secondHalf, (rows / 2) * columns, MPI_INT, 2, tag, MPI_COMM_WORLD);

    int tFirstHalf[tRows][tColumns / 2];
    int tSecondHalf[tRows][tColumns / 2];
    MPI_Recv(tFirstHalf, tRows * tColumns / 2, MPI_INT, 1, tag, MPI_COMM_WORLD, &status);
    MPI_Recv(tSecondHalf, tRows * tColumns / 2, MPI_INT, 2, tag, MPI_COMM_WORLD, &status);

    int tMatrix[tRows][tColumns];
    joinMatrices(tRows, tColumns / 2, tMatrix, tFirstHalf, tSecondHalf);
    printMatrix(tRows, tColumns, tMatrix);

  } else {
    int sRows = rows / 2;
    int sColumns = 2;
    int subMatrix[sRows][sColumns];
    MPI_Recv(subMatrix, sRows * sColumns, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);

    // Transpose matrix
    int subTMatrix[sColumns][sRows];
    transposeMatrix(sRows, sColumns, subMatrix, subTMatrix);

    MPI_Send(subTMatrix, sRows * sColumns, MPI_INT, 0, tag, MPI_COMM_WORLD);
  }

  // Teardown
  MPI_Finalize();

  // Exit
  return 0;
}
Esempio n. 7
0
File: main.c Progetto: no-ox/PCFAces
int main ( int argc, char *argv[] ) {
  // Loop variables
  int i=0, j=0, k=0, l=0;
  
  // The number of faces and their size depend of the database of faces
  int n=0,m=0; //  size of faces
  int N = 0; // number of faces
  
  char **originFacesPath = NULL; 
  Image *originFaces     = NULL;
  Image *eigenFaces      = NULL;
  Image faceAverage;
  
  // ACP elements :
  // we use array of doubles
  double** R           = NULL; // Matrice d'image (une ligne = un visage)
  double** transposedR = NULL; // Matrice d'image transposée
  double* uR           = NULL; // Vecteur moyen
  double** XXT         = NULL;
  // Eigen elements 
  double* eigenValues    = NULL; // the same for XXT and XTX
  double** eigenVectorsV = NULL; // eigen vectors of XXT
  double** eigenVectorsU = NULL; // eigen vectors of XTX
  
  // Face projetction on the new space
  int q; // dimension of the new space
  double** tabCoefficients  = NULL;
  double** eigenProjections = NULL;
  Image *faceProjections  = NULL;
  
  // Output pictures
  char output[OUTPUT_SIZE];
  
  if(argc <= 1){ 
    fprintf(stderr, "Usage : %s image1 image2 ...\n",argv[0]);
    exit (EXIT_FAILURE);
  }
  N = argc - 1;
  
  originFacesPath = parseArgv(argv,N);
  
  printf("Faces importation... ");
  // Importation des N visages sources (matrix nxm) 
  originFaces = importFaces(originFacesPath,N);
  free(originFacesPath);
  free(originFacesPath[0]);
  originFacesPath = NULL;
  
  n = originFaces[0].nbLignes;
  m = originFaces[0].nbColonnes;
  
  // Conversion des visages en vecteurs lignes dans R
  // (première colonne de la matrice puis seconde etc. = m colonnes qui se suivent de n pixels)
  // R = {I1,I2, ... IN}T avec N le nombre de visages (cela donne une matrice de N * (n*m))
  // Un visage dans R correspond à une ligne 
  R = facesConversion(originFaces,N);
  for(i=0 ; i<N ; i++){
    originFaces[i].t2D = imageDesallocation(originFaces[i].t2D);
  }
  free(originFaces);
  printf("Success!\n");
  
  // If it is a center ACP we have to find the average face uR
  uR = averageFace(R,N,(n*m));
  
  // then, for a center ACP, we transform R in a center matrix : R <- R - uR
  centerFacesMatrix(R,uR,N,(n*m));
  matrixExport(uR,n,m,"averageFace.pgm");
  
  // dim(XXT) = N*N != dim(XTX) = (n*m) * (n*m) [BEAUCOUP]
  // Donc on fait l'ACP sur XXT puis on récupère les composantes principales (CP) de XTX grâce aux formules :
  // va = CP de l'axe a de XXT et ua = CP de l'axe A de XTX
  // ua = va * XT * 1/sqrt(lambdaA) avec lambdaA la valeur propre de l'axe A (eigenValues of XXT are equal to the eigenValues of XTX)
  //     a. XXT
  transposedR = transposeMatrix(R,N,(n*m));
  XXT = multiplyMatrix(R,N,(n*m),transposedR,(n*m),N);
  // R = matrixDesallocation(R);
  
  //     b. Valeurs propres et vecteurs propres
  if((eigenValues = malloc(N*sizeof(double)))==NULL){
    perror("Memory allocation error\n");
    exit(EXIT_FAILURE);
  }
  eigenVectorsV = memoryAllocation(N,N);
  printf("ACP... ");
  acp(XXT,N,eigenValues,eigenVectorsV);
  XXT = matrixDesallocation(XXT);
  
  //    c. Now we have to recovery the principal components of XTX
  eigenVectorsU = memoryAllocation(n*m,n*m);
  
  // /!\ we must consider the number of eigen values != 0
  // There are as many eigen values !=0 in XXT as in XTX
  // but the size of these matrix is different
  // so the loop stop before n*m and even N because here N < n*m
  for(j=0 ; j<N ; j++){
    // we multiply the transposedR matrix by the XXT eigen vector of the jth column.
    for(i=0 ; i<n*m ; i++){
      double sum = 0;
      for(k=0 ; k<N ; k++){
	sum += transposedR[i][k]*eigenVectorsV[k][j];
      }
      eigenVectorsU[i][j]=sum/sqrt(eigenValues[j]);
    }
  }
  free(eigenValues);
  transposedR = matrixDesallocation(transposedR);
  eigenVectorsV = matrixDesallocation(eigenVectorsV);
  
  if  ((eigenFaces = malloc(N*sizeof(Image))) == NULL){
    perror("Memory allocation error\n");
    exit(EXIT_FAILURE);
  }
  for(i=0 ; i< N ; i++){
    eigenFaces[i].nbLignes   = n;
    eigenFaces[i].nbColonnes = m;
    eigenFaces[i].t2D        = imageAllocation(n,m);
  }

  //inverseFacesConversion(eigenFaces,eigenVectorsU,N,n,m);
  for(k=0 ; k<N ; k++){
    for(i=0 ; i<n ; i++){
      for(j=0 ; j<m ; j++){
	// /!\ Why a and b ?! without values are too low
	double a = 110;
	double b = 7000;
	if(k==0)
	  printf("%g\t",eigenVectorsU[j*n+i][k]);
	eigenFaces[k].t2D[i][j] = (Pixel) (abs((int) (a+eigenVectorsU[j*n+i][k]*b)));
      }
      	if(k==0)
	  printf("\n");
    }
  }
  // eigenVectorsU = matrixDesallocation(eigenVectorsU);
  /* for(k=0 ; k<N ; k++){
     sprintf(output,"eigen_faces/eigen_face%d.pgm",k);
     matrixExport(eigenVectorsU[][k],n,m,output);
     }*/
  printf("Success!\n");  
  
  printf("# Writing eigen faces ...\n");
  for(i = 0 ; i < N ; i++){ 
    sprintf(output,"img/eigen_faces/eigen_face%d.pgm",i);
    ecrireImage (eigenFaces[i], output);
  }
  
  // desallocation memoire
  for(i=0 ; i<N ; i++){
    eigenFaces[i].t2D = imageDesallocation(eigenFaces[i].t2D);
  }
  
  printf("# Face projection ...\n");
  // face projection
  q = N - 1;
  // each line contains the coefficients of the face in the new q-dimension-space
  tabCoefficients = memoryAllocation(N,q);
  
  eigenProjections = memoryAllocation(N,n*m);
  for(i=0 ; i<N ; i++){
    projection(R[i], eigenVectorsU, uR, n, m, q, tabCoefficients[i], eigenProjections[i]);
  }
  
  // We search the origin face which is the closest of the face to project
  search_face("img/toProject.pgm", eigenVectorsU, uR, n, m, q, N, tabCoefficients);
  tabCoefficients = matrixDesallocation(tabCoefficients);    
  eigenVectorsU   = matrixDesallocation(eigenVectorsU);
  R               = matrixDesallocation(R);
  free(uR);
  uR = NULL;
  
  // Now we have to create faceProjection with the eigenProjections
  /* if((faceProjections=malloc(N*sizeof(Image)))==NULL){
    perror("Memory allocation error !");
    exit(EXIT_FAILURE);
  }
  for(i=0 ; i< N ; i++){
    faceProjections[i].nbLignes   = n;
    faceProjections[i].nbColonnes = m;
    faceProjections[i].t2D        = imageAllocation(n,m);
  }
  for(k=0 ; k<N ; k++){
    for(i=0 ; i<n ; i++){
      for(j=0 ; j<m ; j++){
	faceProjections[k].t2D[i][j] = (Pixel) eigenProjections[k][j*n+i];
      }
    }
    }*/
  for(i=0 ; i<N ; i++){
    sprintf(output,"projections/projection%d.pgm",i);
    matrixExport(eigenProjections[i],n,m,output);
  }
  eigenProjections = matrixDesallocation(eigenProjections);
  
  /*printf("# Writing projection faces ...\n");
  for(i = 0 ; i < N ; i++){ 
    sprintf(output,"img/projections/projection%d.pgm",i);
    ecrireImage (faceProjections[i], output);
  }
  // Memory desallocation
  for(i=0 ; i<N ; i++){
    faceProjections[i].t2D = imageDesallocation(faceProjections[i].t2D);
    }*/
  
  return EXIT_SUCCESS;
}
Esempio n. 8
0
void
gaussianEstimator_Est (Matrix *xEst, Matrix *CEst, Matrix *y, Matrix *Cv, Matrix (*hfun)(Matrix m), Matrix *m_opt)
{
                      //printMatrix(*xEst);
                      //printMatrix(*CEst);system("PAUSE");
Matrix tmp = sizeOfMatrix(*m_opt);                      
float D = elem(tmp,0,1)+1;       //printf("%f\n", D);
freeMatrix(tmp);
float w_opt = 1/D;                                //printf("%f\n", w_opt);
tmp = sizeOfMatrix(*xEst);
float Nx = elem(tmp,0,0);        // printf("%f\n", Nx);
freeMatrix(tmp);
float d = Nx*(D-1) + 1;                           //printf("%f\n", d);
float w = 1/d;                                   // printf("%f\n", w);system("PAUSE");

// Eigenvectors, Eigenvalues
tmp = sizeOfMatrix(*CEst);
int dimC = elem ( tmp, 0, 0 );
freeMatrix(tmp);
Matrix Vec = zeroMatrix(dimC, dimC);    
Matrix Val = zeroMatrix(dimC, dimC);
eig ( CEst, &Vec, &Val );                   //printMatrix(Vec);printMatrix(Val);system("PAUSE");

// m1 = vec*sqrtf(val)
int i;
for ( i = 0; i < dimC; ++i )
    setElem(Val, i, i, sqrtf(fabs(elem(Val, i,i))));
Matrix m1 = mulMatrix(Vec, Val);                   //printMatrix(m1); system("PAUSE");
freeMatrix(Vec);
freeMatrix(Val);

//*  rotate & scale samples: m = m1*S 
Matrix m = scaledSamplePoints(m1, *m_opt);        // printMatrix(m); system("PAUSE");
Matrix mxDiracs = mulScalarMatrix(1, m);


//* x = x*ones(1,d)
Matrix x = fillMatrix(*xEst, d);

// shift samples: m = m + x
tmp = addMatrix(m, x);
appMatrix(m, 0, m->height-1, 0, m->width-1, tmp, 0, tmp->height-1, 0, tmp->width-1 ) ;                              //printMatrix(m);
freeMatrix(tmp);


//% Predicted Measurements
//* hfun 
// yPredDiracs = feval(hfun, m, [], [], t);
// yPred = w*sum(yPredDiracs, 2);

Matrix yPredDiracs = (*hfun) (m);                  //printMatrix(yPredDiracs );  

Matrix yPredDiracsSum = sumMatrix(yPredDiracs, 2); 
Matrix yPred = mulScalarMatrix(w, yPredDiracsSum); 
// myDiracs = yPredDiracs-repmat(yPred, 1, d);
tmp = fillMatrix(yPred, d);
Matrix myDiracs = subMatrix(yPredDiracs, tmp); 
freeMatrix(tmp);

//* CPred = w_opt*mxDiracs*mxDiracs';     
// Matrix CPred = mulScalarMatrix( w_opt, mulMatrix(mxDiracs, transposeMatrix(mxDiracs)) );
// Matrix CPred = *CEst;


// Cxy = w_opt*mxDiracs*myDiracs';
Matrix tmp1 = transposeMatrix(myDiracs);
Matrix tmp2 = mulMatrix(mxDiracs, tmp1);
Matrix Cxy = mulScalarMatrix( w_opt, tmp2);
freeMatrix(tmp1);
freeMatrix(tmp2);


// Cy  = w_opt*myDiracs*myDiracs'+Cv;
tmp1 = transposeMatrix(myDiracs);
tmp2 = mulMatrix(myDiracs, tmp1);
Matrix tmp3 = mulScalarMatrix( w_opt, tmp2);
Matrix Cy = addMatrix( tmp3 , *Cv );
freeMatrix(tmp1);
freeMatrix(tmp2);
freeMatrix(tmp3);

// K = Cxy / Cy;
tmp = invertCovMatrix(Cy);
Matrix K = mulMatrix( Cxy, tmp);
freeMatrix(tmp);

// I = y - yPred;
Matrix I = subMatrix( *y, yPred );

// xEst = xPred + K*I;
tmp = mulMatrix( K, I  );
Matrix tmp23 = addMatrix( *xEst, tmp);
appMatrix(*xEst,0,5,0,0, tmp23,0,5,0,0);
freeMatrix(tmp);


// CEst = CPred - K*Cy*K';
tmp1 = mulMatrix(K, Cy);
tmp2 = transposeMatrix(K);
tmp3 = mulMatrix( tmp1, tmp2);
Matrix tmp24 = subMatrix(*CEst, tmp3);
appMatrix(*CEst,0,5,0,5, tmp24,0,5,0,5);
freeMatrix(tmp1);
freeMatrix(tmp2);
freeMatrix(tmp3);
freeMatrix(tmp24);

freeMatrix(m1);
freeMatrix(m);
freeMatrix(mxDiracs);
freeMatrix(x);
freeMatrix(yPredDiracs);
freeMatrix(yPredDiracsSum);//
freeMatrix(yPred);//
freeMatrix(myDiracs);
freeMatrix(Cxy);
freeMatrix(Cy);
freeMatrix(K);//
freeMatrix(I);//
freeMatrix(tmp23);


}
Esempio n. 9
0
int main(int argc, char** argv)
{
  int rank, size;
  init_app(argc, argv, &rank, &size);

  if (argc < 2) {
    printf("usage: %s <N> [L]\n",argv[0]);
    close_app();
    return 1;
  }

  /* the total number of grid points in each spatial direction is (N+1) */
  /* the total number of degrees-of-freedom in each spatial direction is (N-1) */
  int N  = atoi(argv[1]);
  int M  = N-1;
  double L=1;
  if (argc > 2)
    L = atof(argv[2]);

  double h = L/N;
  Vector lambda = createEigenValues(M);

  Vector grid = createVector(M);
  for (int i=0;i<M;++i)
    grid->data[i] = (i+1)*h;

  Matrix u = createMatrix(M, M);
  Matrix ut = createMatrix(M, M);
  evalMesh(u->as_vec, grid, grid, poisson_source);
  scaleVector(u->as_vec, h*h);

  int NN = 4*N;
  Vector z    = createVector(NN);

  double time = WallTime();

  for (int j=0; j < M; j++)
    fst(u->data[j], &N, z->data, &NN);

  transposeMatrix(ut, u);

  for (int i=0; i < M; i++)
    fstinv(ut->data[i], &N, z->data, &NN);

  for (int j=0; j < M; j++)
    for (int i=0; i < M; i++)
      ut->data[j][i] /= lambda->data[i]+lambda->data[j];

  for (int i=0; i < M; i++)
    fst(ut->data[i], &N, z->data, &NN);

  transposeMatrix(u, ut);

  for (int j=0; j < M; j++)
    fstinv(u->data[j], &N, z->data, &NN);

  evalMesh2(u->as_vec, grid, grid, exact_solution, -1.0);
  double max = maxNorm(u->as_vec);

  if (rank == 0) {
    printf("elapsed: %f\n", WallTime()-time);
    printf("max: %f\n", max);
  }

  freeMatrix(u);
  freeMatrix(ut);
  freeVector(grid);
  freeVector(z);
  freeVector(lambda);

  close_app();
  return 0;
}
Esempio n. 10
0
void reduceRows(int nRows, int nColumns, double* matrix){
	transposeMatrix(nRows, nColumns, matrix);
	reduceColumns(nColumns, nRows, matrix);	
	transposeMatrix(nColumns, nRows/2, matrix);
}
Esempio n. 11
0
int main(int argc, char *argv[])
{
	//double *a = malloc(dim*dim*sizeof(double));
	//double a[9] = {1,0,0,0,1,0,0,0,1};
	//double a[9] = {6,5,4,5,1,4,5,4,3};
	//double a[16] = {1,2,3,4,5,6,3,8,9,3,4,6,3,7,1,9};
	//double a[25] = {1,2,3,4,5,5,6,3,8,9,9,3,4,6,7,3,7,1,9,1,1,2,3,4,5};
	//double a[9] = {1,2,3,4,5,6,3,8,9};
	//double a[9] = {7.8102,4.4813,2.5607,0,-2.4327,3.0729,0,4,3};
	//double a[9] = {5.4,4,7.7,3.5,-.7,2.8,-3.2,5.1,.8};
	//double a[16] = {1,9,8,7,9,2,6,5,8,6,3,1,7,5,1,4};
	
	//choose dimension of matrix here!
	int n = 10;

	//create a test matrix A using rand()
	double *a = malloc(n*n*sizeof(double));
	double *atemp = a;
	int i;
	for(i = 0; i < n*n; i++, atemp++)
	{
		*atemp = ((double)rand()/(double)RAND_MAX);
	}
	
	//allocate space for output matrices B and U
	double *b = malloc(n*n*sizeof(double));
	double *u = malloc(n*n*sizeof(double));

	//run the main process for Hessenberg decomposition
	upperhes(a,n,u,b);

	//create matrix structures from input and output for testing
	Matrix aMat = createMatrix(a,n);
	Matrix bMat = createMatrix(b,n);
	Matrix uMat = createMatrix(u,n);
	Matrix uTran = createMatrix(u,n);
	transposeMatrix(uTran);
	
	//result2 = UAU^T
	Matrix result = initMatrix(n);
	Matrix result2 = initMatrix(n);
	matMul(uMat,aMat,result);
	matMul(result,uTran,result2);

	puts("A = ");
	printMatrix(aMat);
	puts("B = ");
	printMatrix(bMat);
	puts("UAU^T");
	printMatrix(result2);

	//free malloc'ed memory before exiting
	free(a);
	free(b);
	free(u);
	destroyMatrix(aMat);
	destroyMatrix(bMat);
	destroyMatrix(uMat);
	destroyMatrix(uTran);
	destroyMatrix(result);
	destroyMatrix(result2);

	return 0;
}
Esempio n. 12
0
int main(int argc, char **argv) {
    ArgOptionsPrep a = parsePrepOptions(argc, argv);
    Constants *c = createConstants(a.const_filename);

    
    bool saveStat = false;
    if(a.stat_file) {
        saveStat = true;
    }

    pMatrixVector* ts_data = readMatrixListFromFile(a.input_file);
    pMatrixVector* ts_labels = readMatrixListFromFile(a.input_labels_file);
    assert(ts_labels->size == 1);
    assert(ts_labels->array[0]->nrow == ts_data->size);
    
    pMatrixVector *out_data = TEMPLATE(createVector,pMatrix)();
    assert(ts_data->size > 0);

    pMatrixVector *ts_data_pr = processTimeSeriesSet(ts_data, c);    
    indVector *ts_indices = TEMPLATE(createVector,ind)();
    for(size_t ti=0; ti< ts_data_pr->size; ti++) {
        TEMPLATE(insertVector,ind)(ts_indices, ti);    
    }
    srand(time(NULL));
    shuffleIndVector(ts_indices);
    
    SpikesList *net = createSpikesList(c->M);
    doubleVector *ts_labels_current = TEMPLATE(createVector,double)();
    double t = 0;
    doubleVector *timeline = TEMPLATE(createVector,double)();
    AdExLayer *l = createAdExLayer(c->M, saveStat);
    for(size_t ts_i=0; ts_i < ts_indices->size; ts_i++) {
        Matrix *ts = ts_data_pr->array[ ts_indices->array[ts_i] ];
        size_t nsamples = ts->ncol;
        toStartValuesAdExLayer(l, c);
        size_t j;
        for(j = 0; j < nsamples; t+= c->preproc->dt, j++) {
            for(size_t ni=0; ni < l->N; ni++) {
                double I = getMatrixElement(ts, ni, j);
                propagateCurrentAdExLayer(l, &ni, &I);
                simulateAdExLayerNeuron(l, &ni, c);
                if(l->fired[ni] == 1) {
                    TEMPLATE(insertVector,double)(net->list[ni], c->preproc->mult*t);
                    l->fired[ni] = 0;
                }
            }
        }
        t += 250/c->preproc->mult;
        TEMPLATE(insertVector,double)(timeline, c->preproc->mult*t);
        TEMPLATE(insertVector,double)(ts_labels_current, getMatrixElement(ts_labels->array[0], ts_indices->array[ts_i], 0) );
    }
    Matrix *spikes = vectorArrayToMatrix(net->list, net->size);
    TEMPLATE(insertVector,pMatrix)(out_data, spikes);    
    
    Matrix *timeline_m = vectorArrayToMatrix(&timeline, 1);
    transposeMatrix(timeline_m);
    TEMPLATE(insertVector,pMatrix)(out_data, timeline_m);    
    
    Matrix *classes = vectorArrayToMatrix(&ts_labels_current, 1);
    transposeMatrix(classes);
    TEMPLATE(insertVector,pMatrix)(out_data, classes);    

    saveMatrixListToFile(a.output_file, out_data);

    if(l->saveStat) {
        pMatrixVector *stat_data = TEMPLATE(createVector,pMatrix)();
        for(size_t i=0; i<ts_data_pr->size; i++) {
            TEMPLATE(insertVector,pMatrix)(stat_data, copyMatrix(ts_data_pr->array[i]));
        }        
        Matrix *Vs = vectorArrayToMatrix(l->stat_V, l->N);
        TEMPLATE(insertVector,pMatrix)(stat_data, Vs);
        Matrix *ws = vectorArrayToMatrix(l->stat_w, l->N);
        TEMPLATE(insertVector,pMatrix)(stat_data, ws);


        saveMatrixListToFile(a.stat_file, stat_data);

        TEMPLATE(deleteVector,pMatrix)(stat_data);
    }

    TEMPLATE(deleteVector,double)(timeline);
    deleteAdExLayer(l);
    deleteSpikesList(net);
    deleteConstants(c);
    TEMPLATE(deleteVector,pMatrix)(out_data);
    TEMPLATE(deleteVector,pMatrix)(ts_data_pr);
    TEMPLATE(deleteVector,pMatrix)(ts_data);
    return(0);
}
Esempio n. 13
0
void
subspaceTrain (Subspace *s, Matrix images, ImageList *srt, int numSubjects, int dropNVectors, CutOffMode cutOffMode, double cutOff, int useLDA, int writeTextInterm
               /*START Changed by Zeeshan: For LPP*/
               ,int useLPP, int neighbourCount, int useAdaptiveK, int lppKeepNVectors, char* lppDistance
               /*END Changed by Zeeshan: For LPP*/
               /*START 	Changed by Zeeshan: For ICA*/
               ,int useICA, int arch, double learningRate, int blockSize, int iterations
               /*END 	Changed by Zeeshan: For ICA*/
               )
{
    int i;
    Matrix m;
    int n = 0;    /* The number of eigen vectors to keep */
    double total_energy, energy;
    Matrix tmp;

    /* Initialize structure */

    s->useLDA         = useLDA;
    s->cutOffMode     = cutOffMode;
    s->cutOff         = cutOff;
    s->dropNVectors   = dropNVectors;

    s->numSubjects    = numSubjects;
    s->numPixels      = images->row_dim;

    /*START Changed by Zeeshan: For LPP*/
    s->useLPP 	    = useLPP;
    s->neighbourCount = neighbourCount;
    s->lppDistance    = strdup(lppDistance);
    s->lppKeepNVectors= lppKeepNVectors;
    s->useAdaptiveK   = useAdaptiveK;
    /*END Changed by Zeeshan: For LPP*/

    /*START Changed by Zeeshan: For ICA*/
    s->useICA 	    = useICA;
    s->arch	    = arch;
    s->ica2Basis 	    = NULL;
    s->learningRate   = learningRate;
    s->blockSize 	    = blockSize;
    s->iterations     = iterations;
    /*END Changed by Zeeshan: For ICA*/

    /*START Changed by Zeeshan: For ICA & LPP*/
    /*********************************************************************
    * STEP ZERO: Make sure LDA and LPP are executed exclusively
    ********************************************************************/
    DEBUG_CHECK (!(s->useLDA && s->useLPP) && !(s->useLDA && s->useICA) && !(s->useICA && s->useLPP), 
        "Either LDA, LPP or ICA should be executed.");
    /*END Changed by Zeeshan: For ICA & LPP*/


    /*********************************************************************
    * STEP ONE: Calculate the eigenbasis
    ********************************************************************/

    /* Compute the Eigenvalues and Eigenvectors for the covariance matrix
    derived from the images data matrix. The image data is "centered", meaning
    the mean image is subtracted from all images before PCA is performed.
    This centering is done in place, so after this call images are centered. */

    MESSAGE("Computing the PCA eigenspace.");

    eigentrain (&s->mean, &s->values, &s->basis, images);

    MESSAGE("Finished computing eigenspace.");

    /* Numerical roundoff errors may lead to small negative values.
    Strip those before saving the matrix. */

    m = s->values;
    for (i = 0; i < m->row_dim; i++)
    {
        if (ME(m,i,0) < 0) {
            if (ME(m,i,0) < -1e-10)
                printf("WARNING: Large negative eigenvalue found %f. Truncating to zero.\n", ME(m,i,0));
            ME(m,i,0) = 0;
        }
    }

    /*********************************************************************
    * STEP TWO: Drop eigenvectors from the front or truncate them from
    * the back
    ********************************************************************/

    /* 
    The following is used to filter the vectors that are retained after PCA
    training.  The function first optionally removes the vectors from the matrix
    based on the argument -dropNVectors. This argument is always intrepreted in
    terms of absolute numbers, i.e. a value of 1 means drop the first vector, a
    value of 3 means drop the first three. The function then drops vectors from the
    tail based on -cutOffMode and -cutOff arguments. Here, the mode controls how the
    cutoff is performed. The possible modes are:

    NONE:    Keep all remaining eigenvectors.
    SIMPLE:  Keep a percentage where the percentage is specified by cutOff.
    ENERGY:  Keep the fewest vectors are such that the sum of energy for them just
    exceeds cutOff times the total energy. 
    STRETCH: Keep all eigenvectors that have eigenvalues greater than a percentage 
    of the largest, where the percentage is specied by cutOff. 
    CLASSES: Keep as many eigenvectors as there are LDA classes.

    For both Energy and Stretch, if eigen values/vectors are dropped from the front, 
    these are not included in the determination of the total energy or the max
    eigen value. 
    */

    /* Drop the first vectors  */

    DEBUG_CHECK (s->dropNVectors < (s->basis)->col_dim, "Number of vectors to drop must be less than the number of the eigen vectors");

    /* transpose eigenValues for use in this function */

    tmp = transposeMatrix (s->values);
    freeMatrix (s->values);
    s->values = tmp;

    if (s->dropNVectors && (s->dropNVectors < (s->values)->col_dim))
    {
        tmp = matrixCols (s->basis, s->dropNVectors, (s->basis)->col_dim-1);
        freeMatrix (s->basis);
        s->basis = tmp;

        tmp = matrixCols (s->values, s->dropNVectors, (s->values)->col_dim-1);
        freeMatrix (s->values);
        s->values = tmp;
    }

    /* transpose the eigenValues back to the original order. */

    tmp = transposeMatrix (s->values);
    freeMatrix (s->values);
    s->values = tmp;

    DEBUG_CHECK((s->values)->row_dim - s->dropNVectors > 0, "Too many eigen vectors droped from front. Can not proceed.");

    switch (s->cutOffMode)
    {
    case CUTOFF_NONE:
        n = (s->basis)->col_dim;
        break;

    case CUTOFF_SIMPLE:
        n = (int)((s->basis)->col_dim * s->cutOff / 100.0);
        break;

    case CUTOFF_ENERGY:
        /* compute total energy - this will not include vectors/values dropped from front. */
        total_energy = 0;
        for (i = 0; i < (s->values)->row_dim; i++) {
            total_energy += ME(s->values, i, 0);
        }

        /* compute cutoff point */
        i = 0;
        energy = 0;
        while ((i < (s->values)->row_dim) && (energy < total_energy * s->cutOff / 100.0)) {
            energy += ME(s->values, i, 0);
            i++;
        }
        n = i;
        break;

    case CUTOFF_STRETCH:
        i = 1;
        while ((i < (s->values)->row_dim) &&
            (100.0*(ME(s->values, i, 0) / ME(s->values, s->dropNVectors, 0)) > cutOff )) {
                i++;
        }
        n = i;
        break;

    case CUTOFF_CLASSES:
        n = s->numSubjects;
        break;

    case CUTOFF_DROPVEC:
        n = (int)((s->basis)->col_dim - s->cutOff);
        break;

    default:
        n = 0;
        DEBUG_CHECK (0, "ERROR: Unkown cutoff type");
        break;
    };

    /* Never set the dimensionality of the PCA subspace below the number of
    LDA classes when LDA is being used. Doing so creates a horrible problem
    for LDA: too fee dimensions */

    if (s->useLDA && (n < s->numSubjects))
        n = s->numSubjects;

    DEBUG_CHECK (n <= (s->basis)->col_dim, "Tried to expand, not contract, PCA space.");

    MESSAGE1ARG ("Retaining %d eigen vectors.",n);

    tmp = matrixCols ( s->basis, 0 , n-1);
    freeMatrix (s->basis);
    s->basis = tmp;

    DEBUG_INT (1, "Number of eigen vectors kept.", n);

    DEBUG_CHECK ((s->basis)->col_dim > 0, "All basis vectors deleted after cutoff "
        "and vector drop was processed.");

    MESSAGE2ARG("Truncating PCA Space. Subspace projection expressed "
        "as %d by %d matrix.", s->basis->row_dim, s->basis->col_dim);

    /*********************************************************************
    * STEP THREE: Do the LDA if specified
    ********************************************************************/

    if (s->useLDA)
    {
        /* Need to project original images into PCA space */

        Matrix fisherBasis, fisherValues, combinedBasis;
        Matrix imspca = transposeMultiplyMatrixL (s->basis, images);

        MESSAGE("Computing Fisher Linear Discriminants for "
            "training images projected into PCA subspace.");

        fisherTrain (imspca, srt, &fisherBasis, &fisherValues, writeTextInterm);

        combinedBasis = multiplyMatrix (s->basis, fisherBasis);
        basis_normalize (combinedBasis);

        MESSAGE2ARG ("PCA and LDA Combined. Combined projection expressed as %d by "
            "%d matrix.", combinedBasis->row_dim, combinedBasis->col_dim);

        s->values = fisherValues;
        s->basis  = combinedBasis;
    }

    /*START Changed by Zeeshan: For LPP*/
    /*********************************************************************
    * STEP FOUR: Do the LPP if specified
    ********************************************************************/
    if (s->useLPP)
    {
        /* Need to project original images into PCA space */

        Matrix laplacianBasis, laplacianValues, combinedBasis;
        Matrix imspca = transposeMultiplyMatrixL (s->basis, images);           

        MESSAGE("Computing Locality Preservation Projections for "
            "training images projected into PCA subspace.");

        laplacianTrain (imspca, srt, &laplacianBasis, &laplacianValues, neighbourCount, 
            useAdaptiveK, lppKeepNVectors, &s->values, lppDistance, writeTextInterm);

        combinedBasis = multiplyMatrix (s->basis, laplacianBasis);
        basis_normalize (combinedBasis);

        MESSAGE2ARG ("PCA and LPP Combined. Combined projection expressed as %d by "
            "%d matrix.", combinedBasis->row_dim, combinedBasis->col_dim);

        s->values = calculateStandardDeviation (laplacianBasis);//OPTION4
        //s->values = laplacianValues;//OPTION2
        s->basis  = combinedBasis;

        for (i = 0; i < s->values->row_dim; i++)
        {
            if (ME(s->values, i, 0) <= 0.001)
            {
                if (ME(s->values, i, 0) < -1e-10)
                    printf("WARNING: Large negative value found %f. Truncating to zero.\n", ME(s->values, i, 0));
                ME(s->values, i, 0) = 0.001;
            }
        }


    }
    /*END Changed by Zeeshan: For LPP*/


    /*START Changed by Zeeshan: For ICA*/
    /*********************************************************************
    * STEP FIVE: Do the ICA if specified
    ********************************************************************/
    if (s->useICA)
    {
        /* Need to project original images into PCA space */
        Matrix independentBasis;
        Matrix imspca = transposeMultiplyMatrixL (s->basis, images);           

        MESSAGE("Computing independent components from the principle components.");

        independentTrain(s->basis, imspca, &independentBasis, s->arch, s->blockSize, s->learningRate, s->iterations);

        if (s->arch == 1)
        {
            Matrix combinedBasis;
            combinedBasis = multiplyMatrix (s->basis, independentBasis);
            s->basis  = combinedBasis;
            s->ica2Basis = NULL;

            MESSAGE2ARG ("PCA and ICA Combined. Combined projection expressed as %d by "
                "%d matrix.", combinedBasis->row_dim, combinedBasis->col_dim);	
        }
        else if (s->arch == 2)
        {
            s->ica2Basis = independentBasis;
            MESSAGE2ARG ("PCA and ICA kept separate. ICA projection expressed as %d by "
                "%d matrix.", independentBasis->row_dim, independentBasis->col_dim);	
        }
    }
    /*END Changed by Zeeshan: For ICA*/
}
Esempio n. 14
0
void SpeedTest(FunctionCall fc)
{
	int i, min, max, step, sec=0;
	int sizeTimeArray, fct;
	
	char fnc[MAXSIZE_FCT];
	char foutput[256];
	
	struct sigaction action;
	action.sa_handler = handler;
	sigemptyset(&action.sa_mask);
	
	struct timeval start, end, result;
	double maxtime = 0;
	double time;
	double usec;
	double* timeArray;
	
	Matrix a=NULL, b=NULL, tmp=NULL;
	E s;
	int n; 
	
	TokenizeSpeedTest(fc, fnc, &min, &max, &step, &sec);
	
	if (min>0 && max>0 && step>0)
	{
		sizeTimeArray = ((max-min)/step)+1;
		timeArray = (double*)malloc(sizeTimeArray*sizeof(double));
		for (i=0; i<sizeTimeArray; i++) timeArray[i] = 0;
	}
	
	usec = (double)sec * 1000000;
	
	sprintf(foutput, "speedtest_%s_%d_%d_%d_%d", fnc, min, max, step, sec);
	
	fct = GetFunction(fnc);
	
	switch (fct)
	{
		case NMX :
		{
			printf("\t You must specify another function\n");
			return;
		}
		
		case ADD :
		{
			sigaction(SIGINT, &action, NULL);
			
			for (i=min; i<=max; i+=step)
			{
				a = randomMatrix(i, i, MIN_E, MAX_E);
				b = randomMatrix(i, i, MIN_E, MAX_E);
				
				gettimeofday(&start, NULL);
				tmp = addMatricis(a, b);
				gettimeofday(&end, NULL);
				
				timersub(&end, &start, &result);
				time = (result.tv_sec*1000000)+result.tv_usec;
				if (maxtime < time) maxtime = time;
				
				timeArray[(i-min)/step] = time;
				printf("\t* %s Size:%5d ;\tTime:%8.0f µs ;\n", fnc, i, time);
				
				deleteMatrix(a);
				deleteMatrix(b);
				deleteMatrix(tmp);
				
				if (usec >= 1000000) {
					if (time >= usec) {
						i++;
						break;
					}
				}
				if (CTRLC) {
					i++;
					break;
				}
			}
			
			CreateGNUPLOTFile(min, i-step, step, timeArray, maxtime, foutput);
			break;
		}
		
		case SUB :
		{
			sigaction(SIGINT, &action, NULL);
			
			for (i=min; i<=max; i+=step)
			{
				a = randomMatrix(i, i, MIN_E, MAX_E);
				b = randomMatrix(i, i, MIN_E, MAX_E);
				
				gettimeofday(&start, NULL);
				tmp = substractMatricis(a, b);
				gettimeofday(&end, NULL);
				
				timersub(&end, &start, &result);
				time = (result.tv_sec*1000000)+result.tv_usec;
				if (maxtime < time) maxtime = time;
				
				timeArray[(i-min)/step] = time;
				printf("\t* %s Size:%5d ;\tTime:%8.0f µs ;\n", fnc, i, time);
				
				deleteMatrix(a);
				deleteMatrix(b);
				deleteMatrix(tmp);
				
				if (usec >= 1000000) {
					if (time >= usec) {
						i++;
						break;
					}
				}
				if (CTRLC) {
					i++;
					break;
				}
			}
			
			CreateGNUPLOTFile(min, i-step, step, timeArray, maxtime, foutput);
			break;
		}
		
		case MUL :
		{
			sigaction(SIGINT, &action, NULL);
			
			for (i=min; i<=max; i+=step)
			{
				a = randomMatrix(i, i, MIN_E, MAX_E);
				b = randomMatrix(i, i, MIN_E, MAX_E);
				
				gettimeofday(&start, NULL);
				tmp = mulMatricis(a, b);
				gettimeofday(&end, NULL);
				
				timersub(&end, &start, &result);
				time = (result.tv_sec*1000000)+result.tv_usec;
				if (maxtime < time) maxtime = time;
				
				timeArray[(i-min)/step] = time;
				printf("\t* %s Size:%5d ;\tTime:%8.0f µs ;\n", fnc, i, time);
				
				deleteMatrix(a);
				deleteMatrix(b);
				deleteMatrix(tmp);
				
				if (usec >= 1000000) {
					if (time >= usec) {
						i++;
						break;
					}
				}
				if (CTRLC) {
					i++;
					break;
				}
			}
			
			CreateGNUPLOTFile(min, i-step, step, timeArray, maxtime, foutput);
			break;
		}
		
		case MSC :
		{
			sigaction(SIGINT, &action, NULL);
			
			for (i=min; i<=max; i+=step)
			{
				a = randomMatrix(i, i, MIN_E, MAX_E);
				s = mRand(MIN_SCA, MAX_SCA);
				
				gettimeofday(&start, NULL);
				tmp = mult_scal(a, s);
				gettimeofday(&end, NULL);
				
				timersub(&end, &start, &result);
				time = (result.tv_sec*1000000)+result.tv_usec;
				if (maxtime < time) maxtime = time;
				
				timeArray[(i-min)/step] = time;
				printf("\t* %s Size:%5d ;\tTime:%8.0f µs ;\n", fnc, i, time);
				
				deleteMatrix(a);
				deleteMatrix(tmp);
				
				if (usec >= 1000000) {
					if (time >= usec) {
						i++;
						break;
					}
				}
				if (CTRLC) {
					i++;
					break;
				}
			}
			
			CreateGNUPLOTFile(min, i-step, step, timeArray, maxtime, foutput);
			break;
		}
		
		case EXP :
		{
			sigaction(SIGINT, &action, NULL);
			
			for (i=min; i<=max; i+=step)
			{
				a = randomMatrix(i, i, MIN_E, MAX_E);
				n = (int)mRand(MIN_EXP, MAX_EXP);
				
				gettimeofday(&start, NULL);
				tmp = expo(a, n);
				gettimeofday(&end, NULL);
				
				timersub(&end, &start, &result);
				time = (result.tv_sec*1000000)+result.tv_usec;
				if (maxtime < time) maxtime = time;
				
				timeArray[(i-min)/step] = time;
				printf("\t* %s Size:%5d ;\tTime:%8.0f µs ;\n", fnc, i, time);
				
				deleteMatrix(a);
				deleteMatrix(tmp);
				
				if (usec >= 1000000) {
					if (time >= usec) {
						i++;
						break;
					}
				}
				if (CTRLC) {
					i++;
					break;
				}
			}
			
			CreateGNUPLOTFile(min, i-step, step, timeArray, maxtime, foutput);
			break;
		}
		
		case TRA :
		{
			sigaction(SIGINT, &action, NULL);
			
			for (i=min; i<=max; i+=step)
			{
				a = randomMatrix(i, i, MIN_E, MAX_E);
				
				gettimeofday(&start, NULL);
				tmp = transposeMatrix(a);
				gettimeofday(&end, NULL);
				
				timersub(&end, &start, &result);
				time = (result.tv_sec*1000000)+result.tv_usec;
				if (maxtime < time) maxtime = time;
				
				timeArray[(i-min)/step] = time;
				printf("\t* %s Size:%5d ;\tTime:%8.0f µs ;\n", fnc, i, time);
				
				deleteMatrix(a);
				deleteMatrix(tmp);
				
				if (usec >= 1000000) {
					if (time >= usec) {
						i++;
						break;
					}
				}
				if (CTRLC) {
					i++;
					break;
				}
			}
			
			CreateGNUPLOTFile(min, i-step, step, timeArray, maxtime, foutput);
			break;
		}
		
		case DET :
		{
			sigaction(SIGINT, &action, NULL);
			
			for (i=min; i<=max; i+=step)
			{
				a = randomMatrix(i, i, MIN_E, MAX_E);
				
				gettimeofday(&start, NULL);
				s = determinant(a);
				gettimeofday(&end, NULL);
				
				timersub(&end, &start, &result);
				time = (result.tv_sec*1000000)+result.tv_usec;
				if (maxtime < time) maxtime = time;
				
				timeArray[(i-min)/step] = time;
				printf("\t* %s Size:%5d ;\tTime:%8.0f µs ;\n", fnc, i, time);
				
				deleteMatrix(a);
				
				if (usec >= 1000000) {
					if (time >= usec) {
						i++;
						break;
					}
				}
				if (CTRLC) {
					i++;
					break;
				}
			}
			
			CreateGNUPLOTFile(min, i-step, step, timeArray, maxtime, foutput);
			break;
		}
		
		case DLU :
		{
			sigaction(SIGINT, &action, NULL);
			
			for (i=min; i<=max; i+=step)
			{
				a = randomMatrix(i, i, MIN_E, MAX_E);
				b = newMatrix(i, i);
				tmp = identityMatrix(i);
				
				gettimeofday(&start, NULL);
				decomposition(a, b, tmp);
				gettimeofday(&end, NULL);
				
				timersub(&end, &start, &result);
				time = (result.tv_sec*1000000)+result.tv_usec;
				if (maxtime < time) maxtime = time;
				
				timeArray[(i-min)/step] = time;
				printf("\t* %s Size:%5d ;\tTime:%8.0f µs ;\n", fnc, i, time);
				
				deleteMatrix(a);
				deleteMatrix(b);
				deleteMatrix(tmp);
				
				if (usec >= 1000000) {
					if (time >= usec) {
						i++;
						break;
					}
				}
				if (CTRLC) {
					i++;
					break;
				}
			}
			
			CreateGNUPLOTFile(min, i-step, step, timeArray, maxtime, foutput);
			break;
		}
		
		case SOL :
		{
			sigaction(SIGINT, &action, NULL);
			
			for (i=min; i<=max; i+=step)
			{
				a = randomMatrix(i, i, MIN_E, MAX_E);
				b = randomMatrix(i, 1, MIN_E, MAX_E);
				
				gettimeofday(&start, NULL);
				tmp = gauss(a, b);
				gettimeofday(&end, NULL);
				
				timersub(&end, &start, &result);
				time = (result.tv_sec*1000000)+result.tv_usec;
				if (maxtime < time) maxtime = time;
				
				timeArray[(i-min)/step] = time;
				printf("\t* %s Size:%5d ;\tTime:%8.0f µs ;\n", fnc, i, time);
				
				deleteMatrix(a);
				deleteMatrix(b);
				deleteMatrix(tmp);
				
				if (usec >= 1000000) {
					if (time >= usec) {
						i++;
						break;
					}
				}
				if (CTRLC) {
					i++;
					break;
				}
			}
			
			CreateGNUPLOTFile(min, i-step, step, timeArray, maxtime, foutput);
			break;
		}
		
		case INV :
		{
			sigaction(SIGINT, &action, NULL);
			
			for (i=min; i<=max; i+=step)
			{
				a = randomMatrix(i, i, MIN_E, MAX_E);
				
				gettimeofday(&start, NULL);
				tmp = invert(a);
				gettimeofday(&end, NULL);
				
				timersub(&end, &start, &result);
				time = (result.tv_sec*1000000)+result.tv_usec;
				if (maxtime < time) maxtime = time;
				
				timeArray[(i-min)/step] = time;
				printf("\t* %s Size:%5d ;\tTime:%8.0f µs ;\n", fnc, i, time);
				
				deleteMatrix(a);
				deleteMatrix(tmp);
				
				if (usec >= 1000000) {
					if (time >= usec) {
						i++;
						break;
					}
				}
				if (CTRLC) {
					i++;
					break;
				}
			}
			
			CreateGNUPLOTFile(min, i-step, step, timeArray, maxtime, foutput);
			break;
		}
		
		case RNK :
		{
			sigaction(SIGINT, &action, NULL);
			
			for (i=min; i<=max; i+=step)
			{
				a = randomMatrix(i, i, MIN_E, MAX_E);
				
				gettimeofday(&start, NULL);
				n = rank(a);
				gettimeofday(&end, NULL);
				
				timersub(&end, &start, &result);
				time = (result.tv_sec*1000000)+result.tv_usec;
				if (maxtime < time) maxtime = time;
				
				timeArray[(i-min)/step] = time;
				printf("\t* %s Size:%5d ;\tTime:%8.0f µs ;\n", fnc, i, time);
				
				deleteMatrix(a);
				
				if (usec >= 1000000) {
					if (time >= usec) {
						i++;
						break;
					}
				}
				if (CTRLC) {
					i++;
					break;
				}
			}
			
			CreateGNUPLOTFile(min, i-step, step, timeArray, maxtime, foutput);
			break;
		}
		
		case VAR : // default
		case NOF : // default
		default :
		{
			printf("\t%s : Function Not Implemented\n", fnc);
			fni++;
			break;
		}
	}
	
	if (fct!=NOF && fct !=VAR) fni = 0;
	
	free(timeArray);
	CTRLC = 0;
	sigemptyset(&action.sa_mask);
}
Esempio n. 15
0
void CCA_logit(bool perm, 
	       vector<vector<int> > & blperm,
	       Set & S,
	       Plink & P)
  
{

  ///////////////
  // Output results

      ofstream EPI;

      if (!perm)
      {  
	  string f = par::output_file_name+".genepi";
	  P.printLOG("\nWriting gene-based epistasis tests to [ " + f + " ]\n");
	  EPI.open(f.c_str(), ios::out);
	  EPI.precision(4);

	  EPI << setw(12) << "NIND"  << " "
	      << setw(12) << "GENE1"  << " "
	      << setw(12) << "GENE2"  << " "	 
	      << setw(12) << "NSNP1"  << " "
	      << setw(12) << "NSNP2"  << " "
	      << setw(12) << "P" << " "
	      << "\n";

      }


      //////////////////////////////////
      // Canonical correlation analysis

      int ns = P.snpset.size();

      // Consider each pair of genes
      for (int s1=0; s1 < ns-1; s1++)
      {
	for (int s2 = s1+1; s2 < ns; s2++)
        {


	    ////////////////////////////////////////////////////////
	    // Step 1. Construct covariance matrix (cases and controls together)
	    //    And partition covariance matrix:
	    //    S_11  S_21
	    //    S_12  S_22
	      
	    int n1=0, n2=0;
	      
	    vector<vector<double> > sigma(0);
	    vector<double> mean(0);
	    vector<CSNP*> pSNP(0);
	      
	    /////////////////////////////
	    // List of SNPs for both loci
	      
	    for (int l=0; l<P.snpset[s1].size(); l++)
            {
	      if ( S.cur[s1][l] )
	      {
		pSNP.push_back( P.SNP[ P.snpset[s1][l] ] );
		n1++;
	      }
	    }
	    for (int l=0; l<P.snpset[s2].size(); l++)
	    {		
              if ( S.cur[s2][l] )
	      {
		pSNP.push_back( P.SNP[ P.snpset[s2][l] ] );
		n2++;
              }
	    }

	    int n12 = n1 + n2;
	    int ne = n1 < n2 ? n1 : n2;
  
	    ///////////////////////////////////
	    // Construct covariance matrix (cases and controls together)
	      
	    P.setFlags(false);
	    vector<Individual*>::iterator person = P.sample.begin();

	    while ( person != P.sample.end() )
	      {
		(*person)->flag = true;
                person++;
	    }

	      
	    int nind = calcGENEPIMeanVariance(pSNP, 
					      n1,n2,
					      false,
					      &P,
					      mean, 
					      sigma, 
					      P.sample , 
					      blperm[s1],
					      blperm[s2] );
	    


	    ///////////////////////////
	    // Partition covariance matrix
	      
	    vector<vector<double> > I11;
	    vector<vector<double> > I11b;
	    vector<vector<double> > I12;
	    vector<vector<double> > I21;
	    vector<vector<double> > I22;
	    vector<vector<double> > I22b;
	      
	    sizeMatrix( I11, n1, n1);
            sizeMatrix( I11b, n1, n1);
	    sizeMatrix( I12, n1, n2);
	    sizeMatrix( I21, n2, n1);
	    sizeMatrix( I22, n2, n2);
	    sizeMatrix( I22b, n2, n2);             // For step 4b (eigenvectors for gene2)
	      
	    for (int i=0; i<n1; i++)
		for (int j=0; j<n1; j++)
                {
		  I11[i][j] = sigma[i][j];
		  I11b[i][j] = sigma[i][j];
                }
	      
	    for (int i=0; i<n1; i++)
		for (int j=0; j<n2; j++)
		    I12[i][j] = sigma[i][n1+j];
	      
	    for (int i=0; i<n2; i++)
		for (int j=0; j<n1; j++)
		    I21[i][j] = sigma[n1+i][j];
	      
	    for (int i=0; i<n2; i++)
		for (int j=0; j<n2; j++)
                {     
		  I22[i][j] = sigma[n1+i][n1+j];
	          I22b[i][j] = sigma[n1+i][n1+j];
		}


	    ////////////////////////////////////////////////////////
	    // Step 2. Calculate the p x p matrix M1 = inv(sqrt(sig11)) %*% sig12 %*% inv(sig22) %*% sig21 %*% inv(sqrt(sig11))
	    bool flag = true;
	    I11 = msqrt(I11);
	    I11 = svd_inverse(I11,flag);
       	    I22 = svd_inverse(I22,flag);

	    I22b = msqrt(I22b);// For Step 4b
	    I22b = svd_inverse(I22b,flag);
	    I11b = svd_inverse(I11b,flag);
      
	    matrix_t tmp;
	    matrix_t M1;
	      
	    multMatrix(I11, I12, tmp);
            multMatrix(tmp, I22, M1);
	    multMatrix(M1, I21, tmp);
	    multMatrix(tmp, I11, M1);


	    ////////////////////////////////////////////////////////
	    // Step 4a. Calculate the p eigenvalues and p x p eigenvectors of
	    // M (e). These are required to compute the coefficients used to
	    // build the p canonical variates a[k] for gene1 (see below)

            double max_cancor = 0.90;

            // Compute evalues and evectors
            Eigen gene1_eigen = eigenvectors(M1);

    
            // Sort evalues for gene 1. (the first p of these equal the first p of gene 2 (ie M2), if they are sorted)
            vector<double> sorted_eigenvalues_gene1 = gene1_eigen.d;
	    sort(sorted_eigenvalues_gene1.begin(),sorted_eigenvalues_gene1.end(),greater<double>());

            // Position of the largest canonical correlation that is <
            // max_cancor in the sorted vector of eigenvalues.  This will be
            // needed to use the right gene1 and gene2 coefficients to build
            // the appropriate canonical variates. 
            double cancor1=0;
            int cancor1_pos;          
            
            for (int i=0; i<n1; i++)
            {
              if ( sqrt(sorted_eigenvalues_gene1[i]) > cancor1 && sqrt(sorted_eigenvalues_gene1[i]) < max_cancor  )
              {
                cancor1 = sqrt(sorted_eigenvalues_gene1[i]);
                cancor1_pos = i;
                break;
              }
            }

            // Display largest canonical correlation and its position
	    //  cout << "Largest canonical correlation [position]\n"
	    //    << cancor1 << " [" << cancor1_pos << "]" << "\n\n" ;

            // Sort evectors. Rows must be ordered according to cancor value (highest first)
	    matrix_t sorted_eigenvectors_gene1 = gene1_eigen.z;
            vector<int> order_eigenvalues_gene1(n1);

	    for (int i=0; i<n1; i++)
            {
	      // Determine position of the vector associated with the ith cancor
              for (int j=0; j<n1; j++)
	      {
	        if (gene1_eigen.d[j]==sorted_eigenvalues_gene1[i])		 
                {
	          if (i==0)	   
                  {
		    order_eigenvalues_gene1[i]=j;
                    break;
                  }
                  else
                  {
		    if (j!=order_eigenvalues_gene1[i-1])
  		    {
                      order_eigenvalues_gene1[i]=j;
                      break;
                    }
	          }
                }
              }
	    }

            for (int i=0; i<n1; i++)
            {
		sorted_eigenvectors_gene1[i] = gene1_eigen.z[order_eigenvalues_gene1[i]];
	    }
	    //   cout << "Eigenvector matrix - unsorted:\n";
	    // display(gene1_eigen.z);
            //cout << "Eigenvector matrix - sorted:\n";
            //display(sorted_eigenvectors_gene1);


	    ////////////////////////////////////////////////////////
	    // Step 4b. Calculate the q x q eigenvectors of M2 (f). These are
	    // required to compute the coefficients used to build the p
	    // canonical variates b[k] for gene2 (see below). The first p are
	    // given by: f[k] = (1/sqrt(eigen[k])) * inv_sqrt_I22 %*% I21 %*%
	    // inv_sqrt_sig11 %*% e[k] for (k in 1:p) { e.vectors.gene2[,k] =
	    // (1/sqrt(e.values[k])) * inv.sqrt.sig22 %*% sig21 %*%
	    // inv.sqrt.sig11 %*% e.vectors.gene1[,k] }
           
             matrix_t M2;

             multMatrix(I22b, I21, tmp);
             multMatrix(tmp, I11b, M2);
             multMatrix(M2, I12, tmp);
             multMatrix(tmp, I22b, M2);
             Eigen gene2_eigen = eigenvectors(M2);
 
            //cout << "Eigenvalues Gene 2 - unsorted:\n";
            //display(gene2_eigen.d);
 
	    // Sort evalues for gene2
            vector<double> sorted_eigenvalues_gene2 = gene2_eigen.d;
            sort(sorted_eigenvalues_gene2.begin(),sorted_eigenvalues_gene2.end(),greater<double>());

            // Sort eigenvectors for gene2
            matrix_t sorted_eigenvectors_gene2 = gene2_eigen.z;
            vector<int> order_eigenvalues_gene2(n2);

            for (int i=0; i<n2; i++)
            {
		// Determine position of the vector associated with the ith cancor
		for (int j=0; j<n2; j++)
		{
		    if (gene2_eigen.d[j]==sorted_eigenvalues_gene2[i])
		    {
			if (i==0)
			{
			    order_eigenvalues_gene2[i]=j;
			    break;
			}
			else
			{
			    if (j!=order_eigenvalues_gene2[i-1])
			    {
				order_eigenvalues_gene2[i]=j;
				break;
			    }
			}
		    }
		}
            }

	    for (int i=0; i<n2; i++)
            {
                sorted_eigenvectors_gene2[i] = gene2_eigen.z[order_eigenvalues_gene2[i]];
            }

            //cout << "Eigenvector matrix Gene 2 - unsorted:\n";
	    //display(gene2_eigen.z);

            //cout << "Eigenvector matrix Gene 2 - sorted:\n";
            //display(sorted_eigenvectors_gene2);

            //exit(0);

            //////////////////////////////////////////////////////////////////////////////////
	    // Step 5 - Calculate the gene1 (pxp) and gene2 (pxq) coefficients
            // used to create the canonical variates associated with the p
            // canonical correlations

            transposeMatrix(gene1_eigen.z);
	    transposeMatrix(gene2_eigen.z);

	    matrix_t coeff_gene1;
            matrix_t coeff_gene2;

            multMatrix(gene1_eigen.z, I11, coeff_gene1);
	    multMatrix(gene2_eigen.z, I22b, coeff_gene2);

            //cout << "Coefficients for Gene 1:\n";
            //display(coeff_gene1);

            //cout << "Coefficients for Gene 2:\n";
            //display(coeff_gene2);

            //exit(0);

            ///////////////////////////////////////////////////////////////////////
            // Step 6 - Compute the gene1 and gene2 canonical variates
            // associated with the highest canonical correlation NOTE: the
            // original variables of data need to have the mean subtracted  first!
            // Otherwise, the resulting correlation between variate.gene1 and
            // variate.gene1 != estimated cancor.

            // For each individual, eg compos.gene1 =
            // evector.gene1[1]*SNP1.gene1 + evector.gene1[2]*SNP2.gene1 + ...

	    /////////////////////////////////
	    // Consider each SNP in gene1

	    vector<double> gene1(nind);
            
	    for (int j=0; j<n1; j++)
	    {

		CSNP * ps = pSNP[j];


           	///////////////////////////
		// Iterate over individuals

		for (int i=0; i< P.n ; i++)
		{

		    // Only need to look at one perm set
		    bool a1 = ps->one[i];
		    bool a2 = ps->two[i];

		    if ( a1 )
		    {
			if ( a2 ) // 11 homozygote
			{
			    gene1[i] += (1 - mean[j]) * coeff_gene1[order_eigenvalues_gene1[cancor1_pos]][j];
			}
                        else      // 12 
                        {
                            gene1[i] += (0 - mean[j]) * coeff_gene1[order_eigenvalues_gene1[cancor1_pos]][j];
                        }
		    }
		    else
		    {
                      if ( a2 )      // 21
                      {
                        gene1[i] += (0 - mean[j]) * coeff_gene1[order_eigenvalues_gene1[cancor1_pos]][j];
                      }
		      else           // 22 homozygote
		      {
			  gene1[i] += (-1 - mean[j]) * coeff_gene1[order_eigenvalues_gene1[cancor1_pos]][j];
		      }
		    }

		} // Next individual

	    } // Next SNP in gene1

           

            /////////////////////////////////
            // Consider each SNP in gene2
            vector<double> gene2(P.n);
            int cur_snp = -1;            
            for (int j=n1; j<n1+n2; j++)
            {

                cur_snp++;
                CSNP * ps = pSNP[j];


                
		// Iterate over individuals

		for (int i=0; i<P.n; i++)
		{
		    
		    // Only need to look at one perm set
		    bool a1 = ps->one[i];
		    bool a2 = ps->two[i];

		    if ( a1 )
		    {
			if ( a2 ) // 11 homozygote
			{
			    gene2[i] += (1 - mean[j]) * coeff_gene2[order_eigenvalues_gene2[cancor1_pos]][cur_snp];
			}
			else      // 12
			{
			    gene2[i] += (0 - mean[j]) * coeff_gene2[order_eigenvalues_gene2[cancor1_pos]][cur_snp];
			}
		    }
		    else
		    {
			if ( a2 )      // 21
			{
			    gene2[i] += (0 - mean[j]) * coeff_gene2[order_eigenvalues_gene2[cancor1_pos]][cur_snp];
			}
			else           // 22 homozygote
			{
			    gene2[i] += (-1 - mean[j]) * coeff_gene2[order_eigenvalues_gene2[cancor1_pos]][cur_snp];
			}
		    }
		    
		} // Next individual
		
	    } // Next SNP in gene2


            // Store gene1.variate and gene2.variate in the multiple_covariates field of P.sample
	    // TO DO: NEED TO CHECK IF FIELDS ARE EMPTY FIRST!

	    for (int i=0; i<P.n; i++)
	    {
		P.sample[i]->clist.resize(2);
		P.sample[i]->clist[0] = gene1[i];
		P.sample[i]->clist[1] = gene2[i];
	    }

            ///////////////////////////////////////////////
	    // STEP 7 - Logistic or linear regression epistasis test
	    //
	    
	    Model * lm;
	    	   
	    if (par::bt)
	    {
		LogisticModel * m = new LogisticModel(& P);
		lm = m;
	    }
	    else
	    {
		LinearModel * m = new LinearModel(& P);
		lm = m;
	    }

	    // No SNPs used
	    lm->hasSNPs(false);

	    // Set missing data
	    lm->setMissing();

 	    // Main effect of GENE1 1. Assumes that the variable is in position 0 of the clist vector
	    lm->addCovariate(0);
	    lm->label.push_back("GENE1");

	    // Main effect of GENE 2. Assumes that the variable is in position 1 of the clist vector
	    lm->addCovariate(1);
	    lm->label.push_back("GENE2");

	    // Epistasis
	    lm->addInteraction(1,2);
	    lm->label.push_back("EPI");

	    // Build design matrix
	    lm->buildDesignMatrix();

	    // Prune out any remaining missing individuals
// No longer needed (check)
//	    lm->pruneY();

	    // Fit linear model
	    lm->fitLM();


	    // Did model fit okay?
	    lm->validParameters();

	    // Obtain estimates and statistic
	    lm->testParameter = 3; // interaction
	    vector_t b = lm->getCoefs();
	    double chisq = lm->getStatistic();
	    double logit_pvalue = chiprobP(chisq,1);


	    // Clean up
	    delete lm;



            /////////////////////////////
            // OUTPUT

	    EPI << setw(12) << nind  << " "
		<< setw(12) << P.setname[s1]  << " "
                << setw(12) << P.setname[s2] << " "
                << setw(12) << n1  << " "
                << setw(12) << n2 << " "
                << setw(12) << logit_pvalue << " "
                << "\n";


        }  // End of loop over genes2
 

      }  // End of loop over genes1
      

      EPI.close();


}  // End of CCA_logit() 
Esempio n. 16
0
//L-System tree generator
treegen::error make_ltree(ManualMapVoxelManipulator &vmanip, v3s16 p0, INodeDefManager *ndef,
		TreeDef tree_definition)
{
	MapNode dirtnode(ndef->getId("mapgen_dirt"));
	int seed;
	if (tree_definition.explicit_seed)
	{
		seed = tree_definition.seed+14002;
	}
	else
	{
		seed = p0.X*2 + p0.Y*4 + p0.Z;      // use the tree position to seed PRNG
	}
	PseudoRandom ps(seed);

	// chance of inserting abcd rules
	double prop_a = 9;
	double prop_b = 8;
	double prop_c = 7;
	double prop_d = 6;

	//randomize tree growth level, minimum=2
	s16 iterations = tree_definition.iterations;
	if (tree_definition.iterations_random_level>0)
		iterations -= ps.range(0,tree_definition.iterations_random_level);
	if (iterations<2)
		iterations=2;

	s16 MAX_ANGLE_OFFSET = 5;
	double angle_in_radians = (double)tree_definition.angle*M_PI/180;
	double angleOffset_in_radians = (s16)(ps.range(0,1)%MAX_ANGLE_OFFSET)*M_PI/180;

	//initialize rotation matrix, position and stacks for branches
	core::matrix4 rotation;
	rotation = setRotationAxisRadians(rotation, M_PI/2,v3f(0,0,1));
	v3f position;
	position.X = p0.X;
	position.Y = p0.Y;
	position.Z = p0.Z;
	std::stack <core::matrix4> stack_orientation;
	std::stack <v3f> stack_position;

	//generate axiom
	std::string axiom = tree_definition.initial_axiom;
	for(s16 i=0; i<iterations; i++)
	{
		std::string temp = "";
		for(s16 j=0; j<(s16)axiom.size(); j++)
		{
			char axiom_char = axiom.at(j);
			switch (axiom_char)
			{
			case 'A':
				temp+=tree_definition.rules_a;
				break;
			case 'B':
				temp+=tree_definition.rules_b;
				break;
			case 'C':
				temp+=tree_definition.rules_c;
				break;
			case 'D':
				temp+=tree_definition.rules_d;
				break;
			case 'a':
				if (prop_a >= ps.range(1,10))
					temp+=tree_definition.rules_a;
				break;
			case 'b':
				if (prop_b >= ps.range(1,10))
					temp+=tree_definition.rules_b;
				break;
			case 'c':
				if (prop_c >= ps.range(1,10))
					temp+=tree_definition.rules_c;
				break;
			case 'd':
				if (prop_d >= ps.range(1,10))
					temp+=tree_definition.rules_d;
				break;
			default:
				temp+=axiom_char;
				break;
			}
		}
		axiom=temp;
	}

	//make sure tree is not floating in the air
	if (tree_definition.trunk_type == "double")
	{
		tree_node_placement(vmanip,v3f(position.X+1,position.Y-1,position.Z),dirtnode);
		tree_node_placement(vmanip,v3f(position.X,position.Y-1,position.Z+1),dirtnode);
		tree_node_placement(vmanip,v3f(position.X+1,position.Y-1,position.Z+1),dirtnode);
	}
	else if (tree_definition.trunk_type == "crossed")
	{
		tree_node_placement(vmanip,v3f(position.X+1,position.Y-1,position.Z),dirtnode);
		tree_node_placement(vmanip,v3f(position.X-1,position.Y-1,position.Z),dirtnode);
		tree_node_placement(vmanip,v3f(position.X,position.Y-1,position.Z+1),dirtnode);
		tree_node_placement(vmanip,v3f(position.X,position.Y-1,position.Z-1),dirtnode);
	}

	/* build tree out of generated axiom

	Key for Special L-System Symbols used in Axioms

    G  - move forward one unit with the pen up
    F  - move forward one unit with the pen down drawing trunks and branches
    f  - move forward one unit with the pen down drawing leaves (100% chance)
    T  - move forward one unit with the pen down drawing trunks only
    R  - move forward one unit with the pen down placing fruit
    A  - replace with rules set A
    B  - replace with rules set B
    C  - replace with rules set C
    D  - replace with rules set D
    a  - replace with rules set A, chance 90%
    b  - replace with rules set B, chance 80%
    c  - replace with rules set C, chance 70%
    d  - replace with rules set D, chance 60%
    +  - yaw the turtle right by angle degrees
    -  - yaw the turtle left by angle degrees
    &  - pitch the turtle down by angle degrees
    ^  - pitch the turtle up by angle degrees
    /  - roll the turtle to the right by angle degrees
    *  - roll the turtle to the left by angle degrees
    [  - save in stack current state info
    ]  - recover from stack state info

    */

	s16 x,y,z;
	for(s16 i=0; i<(s16)axiom.size(); i++)
	{
		char axiom_char = axiom.at(i);
		core::matrix4 temp_rotation;
		temp_rotation.makeIdentity();
		v3f dir;
		switch (axiom_char)
		{
		case 'G':
			dir = v3f(1,0,0);
			dir = transposeMatrix(rotation,dir);
			position+=dir;
			break;
		case 'T':
			tree_trunk_placement(vmanip,v3f(position.X,position.Y,position.Z),tree_definition);
			if (tree_definition.trunk_type == "double" && !tree_definition.thin_branches)
			{
				tree_trunk_placement(vmanip,v3f(position.X+1,position.Y,position.Z),tree_definition);
				tree_trunk_placement(vmanip,v3f(position.X,position.Y,position.Z+1),tree_definition);
				tree_trunk_placement(vmanip,v3f(position.X+1,position.Y,position.Z+1),tree_definition);
			}
			else if (tree_definition.trunk_type == "crossed" && !tree_definition.thin_branches)
			{
				tree_trunk_placement(vmanip,v3f(position.X+1,position.Y,position.Z),tree_definition);
				tree_trunk_placement(vmanip,v3f(position.X-1,position.Y,position.Z),tree_definition);
				tree_trunk_placement(vmanip,v3f(position.X,position.Y,position.Z+1),tree_definition);
				tree_trunk_placement(vmanip,v3f(position.X,position.Y,position.Z-1),tree_definition);
			}
			dir = v3f(1,0,0);
			dir = transposeMatrix(rotation,dir);
			position+=dir;
			break;
		case 'F':
			tree_trunk_placement(vmanip,v3f(position.X,position.Y,position.Z),tree_definition);
			if ((stack_orientation.empty() && tree_definition.trunk_type == "double") ||
				(!stack_orientation.empty() && tree_definition.trunk_type == "double" && !tree_definition.thin_branches))
			{
				tree_trunk_placement(vmanip,v3f(position.X+1,position.Y,position.Z),tree_definition);
				tree_trunk_placement(vmanip,v3f(position.X,position.Y,position.Z+1),tree_definition);
				tree_trunk_placement(vmanip,v3f(position.X+1,position.Y,position.Z+1),tree_definition);
			}
			else if ((stack_orientation.empty() && tree_definition.trunk_type == "crossed") ||
				(!stack_orientation.empty() && tree_definition.trunk_type == "crossed" && !tree_definition.thin_branches))
			{
				tree_trunk_placement(vmanip,v3f(position.X+1,position.Y,position.Z),tree_definition);
				tree_trunk_placement(vmanip,v3f(position.X-1,position.Y,position.Z),tree_definition);
				tree_trunk_placement(vmanip,v3f(position.X,position.Y,position.Z+1),tree_definition);
				tree_trunk_placement(vmanip,v3f(position.X,position.Y,position.Z-1),tree_definition);
			}
			if (stack_orientation.empty() == false)
			{
				s16 size = 1;
				for(x=-size; x<=size; x++)
					for(y=-size; y<=size; y++)
						for(z=-size; z<=size; z++)
							if (abs(x) == size && abs(y) == size && abs(z) == size)
							{
								tree_leaves_placement(vmanip,v3f(position.X+x+1,position.Y+y,position.Z+z),ps.next(), tree_definition);
								tree_leaves_placement(vmanip,v3f(position.X+x-1,position.Y+y,position.Z+z),ps.next(), tree_definition);
								tree_leaves_placement(vmanip,v3f(position.X+x,position.Y+y,position.Z+z+1),ps.next(), tree_definition);
								tree_leaves_placement(vmanip,v3f(position.X+x,position.Y+y,position.Z+z-1),ps.next(), tree_definition);
							}
			}
			dir = v3f(1,0,0);
			dir = transposeMatrix(rotation,dir);
			position+=dir;
			break;
		case 'f':
			tree_single_leaves_placement(vmanip,v3f(position.X,position.Y,position.Z),ps.next() ,tree_definition);
			dir = v3f(1,0,0);
			dir = transposeMatrix(rotation,dir);
			position+=dir;
			break;
		case 'R':
			tree_fruit_placement(vmanip,v3f(position.X,position.Y,position.Z),tree_definition);
			dir = v3f(1,0,0);
			dir = transposeMatrix(rotation,dir);
			position+=dir;
			break;

		// turtle orientation commands
		case '[':
			stack_orientation.push(rotation);
			stack_position.push(position);
			break;
		case ']':
			if (stack_orientation.empty())
				return UNBALANCED_BRACKETS;
			rotation=stack_orientation.top();
			stack_orientation.pop();
			position=stack_position.top();
			stack_position.pop();
			break;
		case '+':
			temp_rotation.makeIdentity();
			temp_rotation=setRotationAxisRadians(temp_rotation, angle_in_radians+angleOffset_in_radians,v3f(0,0,1));
			rotation*=temp_rotation;
			break;
		case '-':
			temp_rotation.makeIdentity();
			temp_rotation=setRotationAxisRadians(temp_rotation, angle_in_radians+angleOffset_in_radians,v3f(0,0,-1));
			rotation*=temp_rotation;
			break;
		case '&':
			temp_rotation.makeIdentity();
			temp_rotation=setRotationAxisRadians(temp_rotation, angle_in_radians+angleOffset_in_radians,v3f(0,1,0));
			rotation*=temp_rotation;
			break;
		case '^':
			temp_rotation.makeIdentity();
			temp_rotation=setRotationAxisRadians(temp_rotation, angle_in_radians+angleOffset_in_radians,v3f(0,-1,0));
			rotation*=temp_rotation;
			break;
		case '*':
			temp_rotation.makeIdentity();
			temp_rotation=setRotationAxisRadians(temp_rotation, angle_in_radians,v3f(1,0,0));
			rotation*=temp_rotation;
			break;
		case '/':
			temp_rotation.makeIdentity();
			temp_rotation=setRotationAxisRadians(temp_rotation, angle_in_radians,v3f(-1,0,0));
			rotation*=temp_rotation;
			break;
		default:
			break;
		}
	}

	return SUCCESS;
}
Esempio n. 17
0
void
gaussianEstimator_Pred_decomp ( Matrix *xEst, Matrix *CEst, Matrix *U, Matrix *Cw, float *dt, Matrix *m_opt)
{
float r;

Matrix sizeMopt;
Matrix xn = zeroMatrix(3,1);
Matrix Cn = zeroMatrix(3,3);
Matrix xl = zeroMatrix(9,1);
Matrix Cl = zeroMatrix(9,9);
Matrix Cnl = zeroMatrix(3,9);
Matrix Cnl_T;
Matrix Cn_i;
Matrix CLN;
Matrix sizeCn;
Matrix Vec;
Matrix Val;
Matrix m1;
Matrix m;
Matrix x;
Matrix A;
Matrix Hi = zeroMatrix(12,9);
Matrix Cy = zeroMatrix(12, 12);
Matrix muy = zeroMatrix(12, 1);
Matrix zeros33 = zeroMatrix(3,3);
Matrix eye33 = unitMatrix(3,3);
Matrix Mat;
Matrix H;
Matrix gi = zeroMatrix(12,1);
Matrix Rot_vec = zeroMatrix(3,1);
Matrix mui;
Matrix muiy;
Matrix Ciy;
Matrix tmp;
Matrix tmp1;
Matrix tmp2;
Matrix tmp3;
Matrix tmp4;
Matrix tmp5;
Matrix tmp6;
Matrix tmp7;
Matrix tmp8;
Matrix tmpHi; 
                              
sizeMopt = sizeOfMatrix(*m_opt);                      //printf("%f\n",*dt);
float D = elem(sizeMopt,0,1)+1;                       //printf("%f\n", D);
freeMatrix(sizeMopt);
float w_opt = 1/D;                                    //printf("%f\n", w_opt);
float Nx = 3;
float d = Nx*(D-1) + 1;                               //printf("%f\n", d);
float w = 1/d;                                        //printf("%f\n", w);



//xn = xEst(4:6); % Rotation vector
appMatrix(xn, 0, 2, 0, 0, *xEst, 3, 5, 0, 0);         //printMatrix(xn);system("PAUSE");

//Cn = CEst(4:6,4:6);
appMatrix(Cn, 0, 2, 0, 2, *CEst, 3, 5, 3, 5);         //printMatrix(Cn);system("PAUSE");

//xl = [xEst(1:3) ; xEst(7:12)]; % Translation, angular velocity, linear velocity
appMatrix(xl, 0, 2, 0, 0, *xEst, 0, 2, 0, 0);
appMatrix(xl, 3, 8, 0, 0, *xEst, 6, 11, 0, 0);         //printMatrix(xl);system("PAUSE");

//Cl = [CEst(1:3,1:3) CEst(1:3,7:12);
//      CEst(7:12,1:3) CEst(7:12,7:12)] ;
appMatrix(Cl, 0, 2, 0, 2, *CEst, 0, 2, 0, 2);
appMatrix(Cl, 0, 2, 3, 8, *CEst, 0, 2, 6, 11);
appMatrix(Cl, 3, 8, 0, 2, *CEst, 6, 11, 0, 2);
appMatrix(Cl, 3, 8, 3, 8, *CEst, 6, 11, 6, 11);        //printMatrix(Cl);system("PAUSE");

//Cnl = [CEst(4:6,1:3) CEst(4:6,7:12)];
appMatrix(Cnl, 0, 2, 0, 2, *CEst, 3, 5, 0, 2);
appMatrix(Cnl, 0, 2, 3, 8, *CEst, 3, 5, 6, 11);      //printMatrix(Cnl);system("PAUSE");

//CLN = Cl - Cnl'*inv(Cn)*Cnl;
Cnl_T = transposeMatrix(Cnl);
                                    // printMatrix(Cn);system("PAUSE");
Cn_i = invertCovMatrix(Cn);         //printMatrix(Cn_i);system("PAUSE");

tmp = mulMatrix( Cnl_T, Cn_i);
tmp7 = mulMatrix(tmp, Cnl);
CLN = subMatrix ( Cl,  tmp7);                //printMatrix(CLN);system("PAUSE");
freeMatrix(tmp);
freeMatrix(tmp7);

// Eigenvectors, Eigenvalues
sizeCn = sizeOfMatrix(Cn);
int dimC = elem ( sizeCn, 0, 0 );
freeMatrix(sizeCn);
Vec = zeroMatrix(dimC, dimC);    
Val = zeroMatrix(dimC, dimC);

eig ( &Cn, &Vec, &Val );    //printMatrix(Cn);printMatrix(Vec);printMatrix(Val);system("PAUSE");

// m1 = vec*sqrtf(val)
int i;
for ( i = 0; i < dimC; ++i )
    setElem(Val, i, i, sqrtf(fabs(elem(Val, i,i))));
m1 = mulMatrix(Vec, Val);           //printMatrix(m1);system("PAUSE");

//  rotate & scale samples: m = m1*S
m = scaledSamplePoints(m1, *m_opt); //printMatrix(m);system("PAUSE");
// x = x*ones(1,d)
x = fillMatrix(xn, d);
// shift samples: m = m + x
tmp = addMatrix(m, x);
appMatrix(m, 0, m->height-1, 0, m->width-1, tmp, 0, tmp->height-1, 0, tmp->width-1 );     //printMatrix(m);system("PAUSE");
freeMatrix(tmp);
//A = [[eye(3,3),t*eye(3,3)];[zeros(3,3),eye(3,3)]];
A = unitMatrix(6,6);
setElem(A, 0, 3, *dt);
setElem(A, 1, 4, *dt);
setElem(A, 2, 5, *dt);                              //printMatrix(A);system("PAUSE");

for (i=0; i<d; i++)
{
    //gi = [zeros(3,1); m(:,i); zeros(6,1)];
    setElem(gi, 3, 0, elem(m, 0, i));
    setElem(gi, 4, 0, elem(m, 1, i));    
    setElem(gi, 5, 0, elem(m, 2, i));               //printMatrix(gi);system("PAUSE");
    //Rot_vec = m(:,i);
    setElem(Rot_vec, 0, 0, elem(m, 0, i));
    setElem(Rot_vec, 1, 0, elem(m, 1, i));
    setElem(Rot_vec, 2, 0, elem(m, 2, i));          //printMatrix(Rot_vec);system("PAUSE");

    //r = norm(Rot_vec);
    r = sqrtf( powf((elem(Rot_vec,0,0)),2) + powf((elem(Rot_vec,1,0)),2) + powf((elem(Rot_vec,2,0)),2) );  //printf("%f\n",r);

    H = zeroMatrix(3,3);

    if (fmod(r, 2*pi) == 0)
       {
         Mat = unitMatrix(3,3);
       }
        
        
    else
       { 
        // build skew symmetric Matrix
        setElem(H, 0, 1, -elem(Rot_vec,2,0));
        setElem(H, 0, 2,  elem(Rot_vec,1,0));
        setElem(H, 1, 0,  elem(Rot_vec,2,0));
        setElem(H, 1, 2, -elem(Rot_vec,0,0));
        setElem(H, 2, 0, -elem(Rot_vec,1,0));
        setElem(H, 2, 1,  elem(Rot_vec,0,0));      //printMatrix(H);system("PAUSE");
        // Bortz equation 
        // Mat = eye(3,3) + 0.5*H + (1- r*sin(r)/( 2*(1-cos(r))))/r^2*H*H;
        // already declared Mat = unitMatrix(3,3);
        tmp1 = mulScalarMatrix(0.5, H);
        tmp4 = addMatrix( eye33 , tmp1 );
        tmp2 = mulMatrix(H, H);
        tmp3 = mulScalarMatrix( (1-(r*sin(r)/(2*(1-cos(r)))))/powf(r,2), tmp2);
        Mat = addMatrix( tmp4, tmp3);
                                               //printMatrix(Mat);system("PAUSE");
        freeMatrix(tmp1);
        freeMatrix(tmp2);
        freeMatrix(tmp3);
        freeMatrix(tmp4);

       }
    
    //Hi = [[A(1:3,1:3) zeros(3,3) A(1:3,4:6)];
    //     [zeros(3,3), t*Mat, zeros(3,3)];
    //     [zeros(3,3), eye(3,3), zeros(3,3)];
    //     [A(4:6,1:3),zeros(3,3), A(4:6,4:6)]];
    
    appMatrix( Hi, 0, 2, 0, 2, A,       0, 2, 0, 2 );
    appMatrix( Hi, 0, 2, 3, 5, zeros33, 0, 2, 0, 2 );
    appMatrix( Hi, 0, 2, 6, 8, A,       0, 2, 3, 5 );

    appMatrix( Hi, 3, 5, 0, 2, zeros33, 0, 2, 0, 2 );
    tmpHi = mulScalarMatrix(*dt, Mat);
    appMatrix( Hi, 3, 5, 3, 5, tmpHi,     0, 2, 0, 2 );
    freeMatrix(tmpHi);
    appMatrix( Hi, 3, 5, 6, 8, zeros33, 0, 2, 0, 2 );
    
    appMatrix( Hi, 6, 8, 0, 2, zeros33, 0, 2, 0, 2 );
    appMatrix( Hi, 6, 8, 3, 5, eye33,   0, 2, 0, 2 );
    appMatrix( Hi, 6, 8, 6, 8, zeros33, 0, 2, 0, 2 );
    
    appMatrix( Hi, 9, 11, 0, 2, A,       3, 5, 0, 2 );
    appMatrix( Hi, 9, 11, 3, 5, zeros33, 0, 2, 0, 2 );
    appMatrix( Hi, 9, 11, 6, 8, A,       3, 5, 3, 5 );     //printMatrix(Hi);system("PAUSE");
    
    // mui = xl + Cnl'*inv(Cn)*(m(:,i)-xn);   //m(:,i) -> Rot_vec
    tmp = mulMatrix(Cnl_T, Cn_i );
    tmp1 = subMatrix(Rot_vec, xn);
    tmp2 = mulMatrix(tmp, tmp1);
    mui = addMatrix(xl, tmp2);
    freeMatrix(tmp);
    freeMatrix(tmp1);
    freeMatrix(tmp2);                                   //printMatrix(mui);system("PAUSE");
        
    // muiy = gi + Hi * mui;
    tmp = mulMatrix(Hi, mui);
    muiy = addMatrix( gi, tmp);     //printMatrix(muiy);system("PAUSE");
    freeMatrix(tmp);
    
    // Ciy = Hi *CLN *Hi';
    tmp1 = mulMatrix(Hi, CLN);
    tmp2 = transposeMatrix(Hi);
    Ciy = mulMatrix( tmp1, tmp2);  //printMatrix(Ciy);system("PAUSE");
    freeMatrix(tmp1);
    freeMatrix(tmp2);
     
    // Cy = Cy + (w*Ciy + w_opt*muiy*muiy');
    tmp3 = mulScalarMatrix(w, Ciy);
    tmp1 = transposeMatrix(muiy);
    tmp2 = mulMatrix(muiy, tmp1);
    tmp4 = mulScalarMatrix( w_opt, tmp2 );
    tmp5 = addMatrix( tmp3, tmp4 );
    tmp6 = addMatrix( Cy, tmp5);
    appMatrix(Cy,0,Cy->height-1,0,Cy->width-1,tmp6, 0,tmp6->height-1,0,tmp6->width-1);  //printMatrix(Cy);system("PAUSE");
    freeMatrix(tmp1);
    freeMatrix(tmp2);
    freeMatrix(tmp3);
    freeMatrix(tmp4);
    freeMatrix(tmp5);
    freeMatrix(tmp6);

    // muy = muy + w*muiy;
    tmp = mulScalarMatrix( w, muiy );
    tmp2 = addMatrix( muy, tmp ); 
    appMatrix(muy,0,muy->height-1,0,muy->width-1, tmp2, 0, tmp2->height-1, 0, tmp2->width-1);  //printMatrix(muy);system("PAUSE");
    freeMatrix(tmp);
    freeMatrix(tmp2);

    freeMatrix(H);
    freeMatrix(Mat);
    freeMatrix(mui);//
    freeMatrix(muiy);//
    freeMatrix(Ciy);
       
} 


appMatrix(*xEst, 0, 11, 0, 0, muy, 0, 11, 0, 0 );                       //printMatrix(muy);system("PAUSE");

//CEst = Cy - muy*muy' * w_opt/w + Cw;
tmp1 = transposeMatrix(muy);
tmp2 = mulMatrix(muy, tmp1);
tmp5 = mulScalarMatrix( w_opt/w, tmp2 );
tmp6 = subMatrix(Cy, tmp5);
tmp8 = addMatrix( tmp6, *Cw);           //printMatrix(*CEst);system("PAUSE");
appMatrix(*CEst,0,11,0,11, tmp8, 0,11,0,11 );                          //printMatrix(tmp8);system("PAUSE");
freeMatrix(tmp1);
freeMatrix(tmp2);
freeMatrix(tmp5);
freeMatrix(tmp6);
freeMatrix(tmp8);

freeMatrix(muy);//
freeMatrix(zeros33);//
freeMatrix(Vec);
freeMatrix(Val);
freeMatrix(Cy);
freeMatrix(xn);
freeMatrix(Cn);
freeMatrix(xl);
freeMatrix(Cl);//
freeMatrix(Cnl);
freeMatrix(Cnl_T);
freeMatrix(Cn_i);
freeMatrix(CLN);//
freeMatrix(m1);
freeMatrix(m);//
freeMatrix(x);
freeMatrix(A);
freeMatrix(eye33);
freeMatrix(Hi);
freeMatrix(gi);
freeMatrix(Rot_vec);



} /* End gaussianPred_decomp */