dimanche 18 mars 2018

Matrix gives error when random number is used

I wrote a code for multiplying 3 matrix (1*n) * (n*n) * (n*1) here n=2. The components of matrix(11,12,21,22)come from direct calculation in dl function which is just a integration. They go into c() function and after creating components of matrix we multiply three of them in each other. THIS IS A BIG PROBLEM FOR ME.

Running this code gives this error:

mm=np.dot(rdag,Cov) ValueError: shapes (1,2,1) and (2,2) not aligned: 1 (dim 2) != 2 (dim 0)

I could not find any solution to manage to solve this problem.

the code is:

import numpy as np
from math import *
from scipy.integrate import quad
from scipy.integrate import odeint
xx=np.array([0.01,0.012,0.014,0.016])
yy=np.array([32.95388698,33.87900347,33.84214074,34.11856704])
Cov=[[137,168],[28155,-2217]]
z0=0
H0=70
def ant(z,Om,Od):
    return 1/np.sqrt((1+Om*z)-Od)
def dl(n,Om,Od):
    q=quad(ant,0,xx[n],args=(Om,Od))[0]
    fn=q
    return fn
dlv = np.vectorize(dl)
def c(i,Om,Od):   #Function for creating arrays of matrix and myliplying
    f_list = []
    for i in range(2):  #the value '2' reflects matrix size
            f_list.append(dlv(i,Om,Od))
    rdag=[f_list]
    rmat=[[f] for f in f_list]
    mm=np.dot(rdag,Cov)
    zz=np.dot(mm,rmat)
    hhh=np.linalg.det(zz)*0.000001
    return hhh                  
for i in range (1,20):
    R1=np.random.uniform(0,1,i)
    def delta():
        p=(np.sqrt(-2*np.log(R1)))
        return p
    Omn=0.32+delta()
    Odn=1-Omn
    print(c(i,Omn,Odn))

I appreciate your help and your attention




Aucun commentaire:

Enregistrer un commentaire