Exemple #1
0
int cranck(double *const Up1, double const*const U, sConst *const psc,pfLinearOp pfTimeStep, pfBC pfBoundary)
{
    if (strcmp(discret,"real")==0)
    {
        psc->eDir =_X;
        pfTimeStep(psc->pUrhs,U,psc);
        addfields(psc->pUk1,U,psc->pUrhs,0.5*psc->dDt,psc->sSize);
        invMat(psc->pUk1,psc->pUk2,psc->sSize,psc->sInvY);
        pfBoundary(psc->pUk2,psc->sSize);

        psc->eDir =_Y;
        pfTimeStep(psc->pUrhs,psc->pUk2,psc);
        addfields(psc->pUk1,psc->pUk2,psc->pUrhs,0.5*psc->dDt,psc->sSize);
        invMat(psc->pUk1,Up1,psc->sSize,psc->sInvX);
        pfBoundary(Up1,psc->sSize);


    }
    else if (strcmp(discret,"fourier")==0)
    {
        psc->eDir =_XF;
        pfTimeStep(psc->pUrhs,U,psc);
        addfields(psc->pUk1,U,psc->pUrhs,0.5*psc->dDt,psc->sSize);
        addfields(psc->pUk1,U,psc->pdSource,0.5*psc->dDt,psc->sSize);
        invMat(psc->pUk1,Up1,psc->sSize,psc->sInvX);
        pfBoundary(Up1,psc->sSize);
    }

    return 0;
}
Vec3 Transform::transformNormal(const Vec3& normal) {
    Vec3 result;

    result.x = invMat(0, 0)*normal.x + invMat(1, 0)*normal.y + invMat(2, 0)*normal.z;
    result.y = invMat(0, 1)*normal.x + invMat(1, 1)*normal.y + invMat(2, 1)*normal.z;
    result.z = invMat(0, 2)*normal.x + invMat(1, 2)*normal.y + invMat(2, 2)*normal.z;
    return result;
}
int draw(Scene& myScene)
{

    unsigned char* img;
    img = (unsigned char*)tga_create(myScene.width, myScene.height, TGA_TRUECOLOR_24);
    int img_index = 0;
    
    for(int m = 0; m<myScene.height*myScene.width*3; m++)
      img[m]=0;
    
  
    for(int y = 0; y < myScene.height; y++)
      for(int x = 0; x < myScene.width; x++)
      {		Ray viewRay;
		viewRay.o.x = x;
		viewRay.o.y = y;
		viewRay.o.z = -1000.0;
		viewRay.d.x = 0.0;
		viewRay.d.y = 0.0;
		viewRay.d.z = 1.0;
		viewRay.reflected = 0;
		float red = 0, green = 0, blue = 0; 
		float coef = 1.0;
		int level = 0;
		int repeatFlag = 0;
		int lastfrontSphere = -1;
		int frontSphere = -1;
		int lastfrontTriangle = -1;
		int frontTriangle = -1;
		
		do{
			Point inter;
			Point frontInter;
			float t_inter;
			Matrix iMat;
			Ray newviewRay;
			int triangleisFront, sphereisFront;
			int flag;
			
			if(repeatFlag==0)
			{	
				int ret_val_trngl = 0;
				flag = 1;
				float t = 2000.0;
				frontSphere = -1;
				frontTriangle = -1;
				sphereisFront = 0;
				triangleisFront = 0;
				
				for(int i = 0; i<myScene.spheres.size(); i++)
				{	if(myScene.spheres[i].transformed==0)	
					{	if(raySphere(viewRay, myScene.spheres[i], t))
						{	frontSphere = i;
							t_inter = t;
						}
					}
					
					else if(myScene.spheres[i].transformed==1)
					{	if(lastfrontSphere!=i)	
						{	if(viewRay.reflected == 0)
							{	Matrix temp_iMat = invMat(myScene.spheres[i].mat);
								Ray temp_newviewRay;
								temp_newviewRay.o = transformPoint(temp_iMat, viewRay.o);
								temp_newviewRay.o = subPoint(temp_newviewRay.o, myScene.spheres[i].tlate);// we have to sub since it is a inverse transform
								temp_newviewRay.d = transformPoint(temp_iMat, viewRay.d);                 ///////////////*doubt*why???////

								if(raySphere(temp_newviewRay, myScene.spheres[i], t))
								{	    frontSphere = i;
									    iMat = temp_iMat;
									    t_inter = t;
									    newviewRay = temp_newviewRay;
									    newviewRay.reflected = 0;
								}
							}
							else
							{	Matrix temp_iMat = invMat(myScene.spheres[i].mat);
								if(raySphere(viewRay, myScene.spheres[i], t))
								{	frontSphere = i;
									iMat = temp_iMat;
									t_inter = t;
									newviewRay = viewRay;
									newviewRay.reflected = 0;
								}
							}  
						}
					}
				}
				
				viewRay.reflected = 0;
				
				for(int i = 0; i<myScene.triangles.size(); i++)
				{	ret_val_trngl = rayTriangle(viewRay, myScene.triangles[i], inter);
					if((ret_val_trngl==1)&&(lastfrontTriangle!=i))
					{	frontTriangle = i;
						frontInter = inter;
					}
				}
				
				if(frontSphere==-1)
					if(frontTriangle==-1)
					{	flag = 0;
						triangleisFront = 0;
						sphereisFront = 0;
						break;
					}	
					else 
					{	triangleisFront = 1;
						sphereisFront = 0;
					}
					
				if(frontSphere!=-1)
					if(frontTriangle==-1)
					{	triangleisFront = 0;
						sphereisFront = 1;
					}	
					else 
					{	triangleisFront = 1;
						sphereisFront = 1;
					}	
			}
			
			repeatFlag = 0;
			
			if((sphereisFront==1)&&(triangleisFront==0))
			{	
				if(myScene.spheres[frontSphere].transformed==0)
				{	Point tempo = multPoint(t_inter, viewRay.d);
					Point newStart = addPoint(viewRay.o, tempo);
					Point n = subPoint(newStart, myScene.spheres[frontSphere].c);
					float temp = multPoint(n, n);
					if (temp == 0.0f) 
						break;
					temp = 1.0f / sqrtf(temp); 
					n = multPoint(temp, n);			//normal
				
					Material currentMat = myScene.materials[myScene.spheres[frontSphere].materialId];
					
					for (int j = 0; j < myScene.lights.size(); j++) 
					{
						Light light = myScene.lights[j];
						Point dist = subPoint(light.pos, newStart);
						
						if (multPoint(n, dist) <= 0.0f)
						  continue;
						
						float t = sqrtf(multPoint(dist, dist));

						if ( t <= 0.0f )
						  continue;
						  
						Ray lightRay;
						lightRay.o = newStart;
						lightRay.d = multPoint((1/t), dist);
						
						bool inShadow = false; 
						
						for (int k = 0; k < myScene.spheres.size(); k++) 
						{
						    if (raySphere(lightRay, myScene.spheres[k], t)) 
						    {	inShadow = true;
							break;
						    }
						}
						
						if (!inShadow) 
						{    // lambert
						    float lambert = multPoint(n, lightRay.d) * coef;
						    red += lambert * light.col.r * currentMat.r;
						    green += lambert * light.col.g * currentMat.g;
						    blue += lambert * light.col.b * currentMat.b;
						}
						
					}
					
					coef *= currentMat.reflection;
					float reflet = 2.0f * multPoint(viewRay.d, n);
					viewRay.o = newStart;
					Point temp_2 = multPoint(reflet, n);
					viewRay.d = subPoint(viewRay.d, temp_2);
					viewRay.reflected = 0;
					level++;
				}
				
				else if (myScene.spheres[frontSphere].transformed==1)
				{	
					Point tempo = multPoint(t_inter, newviewRay.d);
					Point newStart = addPoint(newviewRay.o, tempo);			//intersection point in sphere space
					Point newStart_2 = transformPoint(myScene.spheres[frontSphere].mat, newStart);//intersection point in ellipsoid space
					newStart_2 = addPoint(newStart_2, myScene.spheres[frontSphere].tlate); 
					
					Point n = subPoint(newStart, myScene.spheres[frontSphere].c);
					float temp = multPoint(n, n);
					if (temp == 0.0f) 
						break;
					temp = 1.0f / sqrtf(temp); 
					n = multPoint(temp, n);
					
					//convert normal to new worldspace
					Matrix iMat_2 = transMat(iMat);
					Point n_orig = n;
					n = transformPoint(iMat_2, n);
					
					Material currentMat = myScene.materials[myScene.spheres[frontSphere].materialId];

					for (int j = 0; j < myScene.lights.size(); j++) 
					{
						Light light = myScene.lights[j];
						Point dist = subPoint(light.pos, newStart_2);
						
						if (multPoint(n, dist) <= 0.0f)
						  continue;
						
						float t2 = sqrtf(multPoint(dist, dist));
						if ( t2 <= 0.0f )
						  continue;
						  
						Ray lightRay;
						lightRay.o = newStart_2;
						lightRay.d = multPoint((1/t2), dist);
						lightRay.reflected = 0;
						
						Point dist_temp = subPoint(light.pos, newStart);
						float t_temp = sqrtf(multPoint(dist_temp, dist_temp));
						
						bool inShadow = false; 
						Ray newlightRay;
						
						for (int k = 0; k < myScene.spheres.size(); k++) 
						{
						    newlightRay.o = newStart;
						    newlightRay.d = transformPoint(iMat, lightRay.d);
						    newlightRay.reflected = 0;
						    
						    if((k!=frontSphere) && (k!=lastfrontSphere))
							if (raySphere(newlightRay, myScene.spheres[k], t_temp)) 
							{	inShadow = true;
								break;
							}
						}
						
						if (!inShadow) 
						{    // lambert
						    float lambert = multPoint(n, lightRay.d) * coef;
						    red += lambert * light.col.r * currentMat.r;
						    green += lambert * light.col.g * currentMat.g;
						    blue += lambert * light.col.b * currentMat.b;
						}
						
						
					}
					lastfrontSphere = frontSphere;
					coef *= currentMat.reflection;
					float reflet = 2.0f * multPoint(newviewRay.d, n_orig);
					viewRay.o = newStart_2;
					Point temp_2 = multPoint(reflet, n_orig);
					viewRay.d = subPoint(newviewRay.d, temp_2);
					viewRay.reflected = 1;
					level++;
				}
			}

			else if((sphereisFront==0)&&(triangleisFront==1))
			{
				Point u_n = subPoint(myScene.triangles[frontTriangle].v1, frontInter);
				Point v_n = subPoint(myScene.triangles[frontTriangle].v2, frontInter);
				Point n = crossProduct(v_n, u_n);		//pay attention here
				
				float temp = multPoint(n, n);
				if (temp == 0.0f) 
					flag = 0;

				temp = 1.0f / sqrtf(temp); 
				n = multPoint(temp, n);			//normal
				
				Material currentMat = myScene.materials[myScene.triangles[frontTriangle].materialId];
				
				for (int j = 0; j < myScene.lights.size(); j++) 
				{
					Light light = myScene.lights[j];
					Point dist = subPoint(light.pos, frontInter);
					
					if (multPoint(n, dist) <= 0.0f)
					  continue;
					
					float t = sqrtf(multPoint(dist, dist));

					if ( t <= 0.0f )
					  continue;
					  
					Ray lightRay;
					lightRay.o = frontInter;
					lightRay.d = multPoint((1/t), dist);
					
					Point temp_inter;
					
					for (int k = 0; k < myScene.triangles.size(); k++) 
					{
					    if((k!=frontTriangle) && (k!=lastfrontTriangle))
						if ((rayTriangle(lightRay, myScene.triangles[k], temp_inter)==1)) 
						{   flag = 0;
						    break;
						}
					}
					
					if(flag == 1)
					{	float lambert = multPoint(n, lightRay.d) * coef;
						red += lambert * light.col.r * currentMat.r;
						green += lambert * light.col.g * currentMat.g;
						blue += lambert * light.col.b * currentMat.b;
					}
					
					lastfrontTriangle = frontTriangle;	//important
					
				}
				
				coef *= currentMat.reflection;
				float reflet = 2.0f * multPoint(viewRay.d, n);
				viewRay.o = frontInter;
				Point temp_2 = multPoint(reflet, n);
				viewRay.d = subPoint(viewRay.d, temp_2);
				viewRay.reflected = 1;
				level++;
			}
			
			else if((sphereisFront==1)&&(triangleisFront==1))
			{
				Point tempo = multPoint(t_inter, viewRay.d);
				Point newStart = addPoint(viewRay.o, tempo);
				
				if(newStart.z <= frontInter.z)
					triangleisFront=0;
				else
					sphereisFront = 0;
			  
				repeatFlag = 1;
			}
			  
			
		}while((coef>0.0f)&&(level<10));
		
		img[img_index] = (unsigned char)(min(red*255.0f, 255.0f));
		img[img_index+1] = (unsigned char)(min(green*255.0f, 255.0f));
		img[img_index+2] = (unsigned char)(min(blue*255.0f, 255.0f));
		img_index = img_index+3;
      }
 
  int ret_value = tga_write_rle( "output3.tga", myScene.width, myScene.height, img, TGA_TRUECOLOR_24); 
  return 1;
}
Exemple #4
0
static PyObject *UtilsC_expectation_H(PyObject *self, PyObject *args){
    PyObject *XGamma,*Y,*y_tilde,*m_A,*m_H,*X,*R,*Gamma,*Sigma_A,*sigma_epsilone,*Sigma_H,*QQ_barnCond;
    PyArrayObject *XGammaarray,*QQ_barnCondarray,*Yarray,*y_tildearray,*Rarray,*m_Aarray,*m_Harray,*sigma_epsilonearray,*Sigma_Harray,*Xarray,*Gammaarray,*Sigma_Aarray;
    int j,J,m,m2,d,D,nCond,Nrep,nr;
    npy_float64 scale,sigmaH;
    PyArg_ParseTuple(args, "OOOOOOOOOOOOiiiidd",&XGamma,&QQ_barnCond,&sigma_epsilone,&Gamma,&R,&Sigma_H,&Y,&y_tilde,&m_A,&m_H,&Sigma_A,&X,&J,&D,&nCond,&Nrep,&scale,&sigmaH);
    Xarray   = (PyArrayObject *) PyArray_ContiguousFromObject(X,PyArray_INT,3,3);
    m_Aarray = (PyArrayObject *) PyArray_ContiguousFromObject(m_A,PyArray_FLOAT64,2,2);
    Rarray = (PyArrayObject *) PyArray_ContiguousFromObject(R,PyArray_FLOAT64,2,2);
    m_Harray = (PyArrayObject *) PyArray_ContiguousFromObject(m_H,PyArray_FLOAT64,1,1);
    y_tildearray = (PyArrayObject *) PyArray_ContiguousFromObject(y_tilde,PyArray_FLOAT64,2,2);
    sigma_epsilonearray = (PyArrayObject *) PyArray_ContiguousFromObject(sigma_epsilone,PyArray_FLOAT64,1,1);
    Yarray   = (PyArrayObject *) PyArray_ContiguousFromObject(Y,PyArray_FLOAT64,2,2);
    Sigma_Harray   = (PyArrayObject *) PyArray_ContiguousFromObject(Sigma_H,PyArray_FLOAT64,2,2);
    Gammaarray   = (PyArrayObject *) PyArray_ContiguousFromObject(Gamma,PyArray_FLOAT64,2,2);
    Sigma_Aarray = (PyArrayObject *) PyArray_ContiguousFromObject(Sigma_A,PyArray_FLOAT64,3,3);
    QQ_barnCondarray = (PyArrayObject *) PyArray_ContiguousFromObject(QQ_barnCond,PyArray_FLOAT64,4,4);
    XGammaarray = (PyArrayObject *) PyArray_ContiguousFromObject(XGamma,PyArray_FLOAT64,3,3);
    npy_float64 *S,*H,*tmp,*tmpT,*GGamma,*tmp2;
    npy_float64 *yy_tilde,*Y_bar_tilde,*Q_bar,*Q_barnCond;
    struct timeval tstart, tend;

    S = malloc(sizeof(npy_float64)*Nrep);
    H = malloc(sizeof(npy_float64)*D);
    tmp = malloc(sizeof(npy_float64)*Nrep*D);
    tmpT = malloc(sizeof(npy_float64)*Nrep*D);
    GGamma = malloc(sizeof(npy_float64)*Nrep*Nrep);
    tmp2 = malloc(sizeof(npy_float64)*Nrep*D);
    yy_tilde = malloc(sizeof(npy_float64)*Nrep);
    Y_bar_tilde = malloc(sizeof(npy_float64)*D);
    Q_bar = malloc(sizeof(npy_float64)*D*D);
    Q_barnCond = malloc(sizeof(npy_float64)*nCond*nCond*D*D);

    for (d=0;d<Nrep;d++){
        for (nr=0;nr<Nrep;nr++){
            GGamma[d*Nrep + nr] = GetValue(Gammaarray,nr,d);
        }
    }
    for (d=0;d<D;d++){
        for (nr=0;nr<D;nr++){
            Q_bar[d*D+nr] = GetValue(Rarray,d,nr) * scale/sigmaH;
        }
        Y_bar_tilde[d] = 0;
    }
    gettimeofday(&tstart, NULL);
    for (j=0;j<J;j++){
        if (GetValue1D(sigma_epsilonearray,j) < eps) GetValue1D(sigma_epsilonearray,j) = eps;
        for (nr=0;nr<Nrep;nr++){
            yy_tilde[nr] = GetValue(y_tildearray,nr,j);
            for (d=0;d<D;d++){   
                tmp[nr*D + d] = 0;
                tmp2[nr*D + d] = 0;
                tmpT[nr*D + d] = 0;
            }
        }
        for (m=0;m<nCond;m++){
            for (nr=0;nr<Nrep;nr++){  
                for (d=0;d<D;d++){
                    tmp2[d*Nrep + nr] += GetValue3D(XGammaarray,m,d,nr) * GetValue(m_Aarray,j,m) / GetValue1D(sigma_epsilonearray,j);
                    tmpT[d*Nrep+nr] += GetValue(m_Aarray,j,m) * GetValue3DInt(Xarray,m,nr,d);
                }
            }
        }
        prodaddMat(tmp2,yy_tilde,Y_bar_tilde,D,1,Nrep);
        for (m=0;m<nCond;m++){
            for (m2=0;m2<nCond;m2++){
                for (d=0;d<D;d++){
                    for (nr=0;nr<D;nr++){
                        Q_bar[d*D+nr] += GetValue4D(QQ_barnCondarray,m,m2,d,nr) * ( GetValue3D(Sigma_Aarray,m,m2,j) + GetValue(m_Aarray,j,m2) * GetValue(m_Aarray,j,m)) / GetValue1D(sigma_epsilonearray,j) ;
                    }
                }
            }
        }
    }
    gettimeofday(&tend, NULL);
    if (profiling)
        printf ("Ytilde and Q_bar step took %ld msec\n", difftimeval(&tend,&tstart));

    gettimeofday(&tstart, NULL);
    invMat(Q_bar,D);
    gettimeofday(&tend, NULL);
    if (profiling)
        printf ("Inv Q_bar step took %ld msec\n", difftimeval(&tend,&tstart));

    gettimeofday(&tstart, NULL);
    prodMat2(Q_bar,Y_bar_tilde,H,D,1,D);
    gettimeofday(&tend, NULL);
    if (profiling)
        printf ("Prod Q_bar Y_bar step took %ld msec\n", difftimeval(&tend,&tstart));

    for (d=0;d<D;d++){
        GetValue1D(m_Harray,d) = H[d];
        for (m=0;m<D;m++){
            GetValue(Sigma_Harray,d,m) = Q_bar[d*D+m];
        }
    }
    
    free(S);
    free(H);
    free(tmp);
    free(tmpT);
    free(GGamma);
    free(tmp2);
    free(yy_tilde);
    free(Y_bar_tilde);
    free(Q_bar);
    free(Q_barnCond);
    Py_DECREF(QQ_barnCondarray);
    Py_DECREF(XGammaarray);
    Py_DECREF(Sigma_Harray);
    Py_DECREF(Sigma_Aarray);
    Py_DECREF(m_Aarray);
    Py_DECREF(m_Harray);
    Py_DECREF(Xarray);
    Py_DECREF(Yarray);
    Py_DECREF(Rarray);
    Py_DECREF(y_tildearray);
    Py_DECREF(Gammaarray);
    Py_DECREF(sigma_epsilonearray);
    Py_INCREF(Py_None);
    return Py_None;
}   
Exemple #5
0
static PyObject *UtilsC_expectation_A(PyObject *self, PyObject *args){
    PyObject *Y,*y_tilde,*m_A,*m_H,*X,*Gamma,*Sigma_A,*sigma_epsilone,*Sigma_H,*PL,*mu_MK,*sigma_MK,*q_Z;
    PyArrayObject *q_Zarray,*mu_MKarray,*sigma_MKarray,*PLarray,*Yarray,*y_tildearray,*m_Aarray,*m_Harray,*sigma_epsilonearray,*Sigma_Harray,*Xarray,*Gammaarray,*Sigma_Aarray;
    int j,J,m,m2,d,D,nCond,Nrep,nr,K,k;
    PyArg_ParseTuple(args, "OOOOOOOOOOOOOiiiii",&q_Z,&mu_MK,&sigma_MK,&PL,&sigma_epsilone,&Gamma,&Sigma_H,&Y,&y_tilde,&m_A,&m_H,&Sigma_A,&X,&J,&D,&nCond,&Nrep,&K);
    Xarray   = (PyArrayObject *) PyArray_ContiguousFromObject(X,PyArray_INT,3,3);
    Sigma_Harray = (PyArrayObject *) PyArray_ContiguousFromObject(Sigma_H,PyArray_FLOAT64,2,2);
    m_Aarray = (PyArrayObject *) PyArray_ContiguousFromObject(m_A,PyArray_FLOAT64,2,2);
    mu_MKarray = (PyArrayObject *) PyArray_ContiguousFromObject(mu_MK,PyArray_FLOAT64,2,2);
    sigma_MKarray = (PyArrayObject *) PyArray_ContiguousFromObject(sigma_MK,PyArray_FLOAT64,2,2);
    PLarray = (PyArrayObject *) PyArray_ContiguousFromObject(PL,PyArray_FLOAT64,2,2);
    m_Harray = (PyArrayObject *) PyArray_ContiguousFromObject(m_H,PyArray_FLOAT64,1,1);
    y_tildearray = (PyArrayObject *) PyArray_ContiguousFromObject(y_tilde,PyArray_FLOAT64,2,2);
    sigma_epsilonearray = (PyArrayObject *) PyArray_ContiguousFromObject(sigma_epsilone,PyArray_FLOAT64,1,1);
    Yarray   = (PyArrayObject *) PyArray_ContiguousFromObject(Y,PyArray_FLOAT64,2,2);
    Gammaarray   = (PyArrayObject *) PyArray_ContiguousFromObject(Gamma,PyArray_FLOAT64,2,2);
    Sigma_Aarray = (PyArrayObject *) PyArray_ContiguousFromObject(Sigma_A,PyArray_FLOAT64,3,3);
    q_Zarray = (PyArrayObject *) PyArray_ContiguousFromObject(q_Z,PyArray_FLOAT64,3,3); 
    npy_float64 *H,*tmp,*tmpT,*GGamma,*tmp2,*X_tilde,*Sigma_Aj,*Delta,*tmpDD,*tmpD,*tmpNrep,*tmpNrep2,*tmpnCond,*tmpnCond2;
    npy_float64 *yy_tilde,*SSigma_H,*Sigma_A0;
    int *XX,*XXT;
    tmpnCond = malloc(sizeof(npy_float64)*nCond);
    tmpnCond2 = malloc(sizeof(npy_float64)*nCond);
    SSigma_H = malloc(sizeof(npy_float64)*D*D);
    Delta = malloc(sizeof(npy_float64)*nCond*nCond);
    Sigma_A0 = malloc(sizeof(npy_float64)*nCond*nCond);
    Sigma_Aj = malloc(sizeof(npy_float64)*nCond*nCond);
    X_tilde = malloc(sizeof(npy_float64)*nCond*D);
    H = malloc(sizeof(npy_float64)*D);
    tmp = malloc(sizeof(npy_float64)*Nrep*D);
    tmpD = malloc(sizeof(npy_float64)*D);
    tmpDD = malloc(sizeof(npy_float64)*D*D);
    tmpNrep = malloc(sizeof(npy_float64)*Nrep);
    tmpNrep2 = malloc(sizeof(npy_float64)*Nrep);
    tmpT = malloc(sizeof(npy_float64)*Nrep*D);
    GGamma = malloc(sizeof(npy_float64)*Nrep*Nrep);
    tmp2 = malloc(sizeof(npy_float64)*Nrep*D);
    yy_tilde = malloc(sizeof(npy_float64)*Nrep);
    XX = malloc(sizeof(int)*D*Nrep);
    XXT = malloc(sizeof(int)*D*Nrep);

    for (d=0;d<Nrep;d++){
        for (nr=0;nr<Nrep;nr++){
            GGamma[d*Nrep + nr] = GetValue(Gammaarray,nr,d);
        }
    }
    for (d=0;d<D;d++){
        H[d] = GetValue1D(m_Harray,d);
        for (nr=0;nr<D;nr++){
            SSigma_H[d*D+nr] = GetValue(Sigma_Harray,d,nr);
        }
    }
    for (m=0;m<nCond;m++){
        for (m2=0;m2<nCond;m2++){
            for (d=0;d<D;d++){
                for (nr=0;nr<Nrep;nr++){
                    XX[nr*D + d] = GetValue3DInt(Xarray,m2,nr,d);
                    XXT[d*Nrep + nr] = GetValue3DInt(Xarray,m,nr,d);
                }
            }
            prodMat4(H,XXT,tmpNrep,1,Nrep,D);
            prodMat2(tmpNrep,GGamma,tmpNrep2,1,Nrep,Nrep);
            prodMat4(tmpNrep2,XX,tmpD,1, D, Nrep);
            Sigma_A0[m*nCond + m2] = prodMatScal(tmpD,H,D);
            prodMat4(SSigma_H,XXT,tmp,D,Nrep,D);
            prodMat2(tmp,GGamma,tmpT,D,Nrep,Nrep);
            prodMat4(tmpT,XX,tmpDD,D, D, Nrep);
            Sigma_A0[m*nCond + m2] += traceMat(tmpDD,D);
            Delta[m*nCond+m2] = 0;
        }
    }
    for (j=0;j<J;j++){
        if (GetValue1D(sigma_epsilonearray,j) < eps) GetValue1D(sigma_epsilonearray,j) = eps;
        for (nr=0;nr<Nrep;nr++){
            yy_tilde[nr] = GetValue(y_tildearray,nr,j);
        }
        for (m=0;m<nCond;m++){
            for (d=0;d<D;d++){
                for (nr=0;nr<Nrep;nr++){
                    XX[nr*D + d] = GetValue3DInt(Xarray,m,nr,d);
                }
            }
            prodMat2(GGamma,yy_tilde,tmpNrep,Nrep,1,Nrep);
            prodMat4(tmpNrep,XX,tmpD,1,D,Nrep);
            for (d=0;d<D;d++){ 
                X_tilde[m*D+d] = tmpD[d] / GetValue1D(sigma_epsilonearray,j);
            }
            for (m2=0;m2<nCond;m2++){
                Sigma_Aj[m*nCond + m2] = Sigma_A0[m*nCond + m2] / GetValue1D(sigma_epsilonearray,j);
            }
        }
        prodMat2(X_tilde,H,tmpnCond,nCond,1,D);
        for (k=0;k<K;k++){
            for (m=0;m<nCond;m++){
        //         Delta[m*nCond+m] = GetValue3D(q_Zarray,m,k,j) / (GetValue(sigma_MKarray,m,k) + eps);
                Delta[m*nCond+m] = GetValue3D(q_Zarray,m,k,j) / GetValue(sigma_MKarray,m,k);
                tmpnCond2[m] = GetValue(mu_MKarray,m,k);
            }
            prodaddMat(Delta,tmpnCond2,tmpnCond,nCond,1,nCond);
            addMatfast(Delta,Sigma_Aj,nCond,nCond);
        }
        invMat(Sigma_Aj,nCond);
        for (m=0;m<nCond;m++){
            for (m2=0;m2<nCond;m2++){
                GetValue3D(Sigma_Aarray,m,m2,j) = Sigma_Aj[m*nCond + m2];
            }
        }
        prodMat2(Sigma_Aj,tmpnCond,tmpnCond2,nCond,1,nCond);
        for (m=0;m<nCond;m++){
            GetValue(m_Aarray,j,m) = tmpnCond2[m];
        }
    }

    free(X_tilde);
    free(Sigma_Aj);
    free(Sigma_A0);
    free(Delta);
    free(tmpDD);
    free(tmpD);
    free(tmpNrep);
    free(tmpNrep2);
    free(tmpnCond);
    free(tmpnCond2);
    free(H);
    free(tmp);
    free(tmpT);
    free(GGamma);
    free(tmp2);
    free(yy_tilde);
    free(XX);
    free(XXT);

    Py_DECREF(q_Zarray);
    Py_DECREF(Sigma_Harray);
    Py_DECREF(Sigma_Aarray);
    Py_DECREF(m_Aarray);
    Py_DECREF(m_Harray);
    Py_DECREF(Xarray);
    Py_DECREF(Yarray);
    Py_DECREF(sigma_MKarray);
    Py_DECREF(mu_MKarray);
    Py_DECREF(PLarray);
    Py_DECREF(y_tildearray);
    Py_DECREF(Gammaarray);
    Py_DECREF(sigma_epsilonearray);
    Py_INCREF(Py_None);
    return Py_None;
}