Please help! me with my helloworld

11 views
Skip to first unread message

Richard Gomes

unread,
Nov 19, 2012, 7:24:43 PM11/19/12
to copperhe...@googlegroups.com
Hello folks,

Could someone point out why the code below fails to compile?
Also, it it correct/recommended to declare explicit Float return types?
Also, it it recommended to try to enforce static typing everywhere using @cutype ?
Any other recommendations?

Any enlightenment on these questions is much appreciated! :)

Thanks a lot,

Richard Gomes
http://launchpad.net/~frgomes

--------------------------------------------------------------------------------------------

from copperhead import *
import numpy as np


 #######################################
#
# low level functions
#####

@cutype("([v1], m1, [v2], m2, n) -> Float")
@cu
def cvar__(v1, m1, v2, m2, n):
    return sum(map(lambda x1,x2: (x1-m1)*(x2-m2), v1, v2)) / float32(n)

@cutype("([v], m, n) -> Float")
@cu
def var__(v, m, n):
    return cvar__(v, m, v, m, n)

@cutype("([v], m, n) -> Float")
@cu
def stddev__(v, m, n):
    return sqrt( var__(v, m, n) )

@cutype("([v1], m1, [v2], m2, n) -> Float")
@cu
def corr__(v1, m1, v2, m2, n):
    return cvar__(v1, m1, v2, m2, n) / ( stddev__(v1, m1, n) * stddev__(v2, m2, n) )


 #######################################
#
# high level functions
#####

@cutype("([v]) -> Float")
@cu
def mean_(v):
    return sum(v) / float32(len(v))

@cutype("([v1], [v2]) -> Float")
@cu
def cvar_(v1, v2):
    return cvar__( v1, mean_(v1), v2, mean_(v2), len(v1) )

@cutype("([v]) -> Float")
@cu
def var_(v):
    return var__(v, mean_(v), len(v))

@cutype("([v]) -> Float")
@cu
def svar_(v):
    return var__(v, mean_(v), len(v)-1)

@cutype("([v]) -> Float")
@cu
def stddev_(v):
    return sqrt( var__(v, mean_(v), len(v)) )

@cutype("([v]) -> Float")
@cu
def sstddev_(v):
    return sqrt( var__(v, mean_(v), len(v)-1) )

@cutype("([v1], [v2]) -> Float")
@cu
def corr_(v1, v2):
    return cvar_(v1, v2) / ( stddev_(v1) * stddev_(v2) )





def rand_floats(n, min, max):
    diff = np.float32(max) - np.float32(min)
    rands = np.array(np.random.random(n), dtype=np.float32)
    rands = rands * diff
    rands = rands + np.float32(min)
    return cuarray(rands)



n = int( raw_input('n: ') )
a1 = rand_floats(n, .65, .98)
a2 = rand_floats(n, .68, .95)


s1 = sum(a1)
s2 = sum(a2)
print 'sum(a1)=', s1
print 'sum(a2)=', s2

m1 = mean_(a1)
m2 = mean_(a2)
print 'mean(a1)=', m1
print 'mean(a2)=', m2

v1 = var_(a1)
v2 = var__(a2, m2, n)
print 'var(a1)=', v1
print 'var(a2, m2, n)=', v2

s1 = stddev_(a1)
s2 = stddev__(a2, m2, n)
print 'stddev(a1)=', s1
print 'stddev(a2, m2, n)=', s2

c = cvar_(v1, v2)
print 'cvar(v1, v2)=', c
c = cvar__(v1, m1, v2, m2, n)
print 'cvar(v1, m1, v2, m2, n)=', c

r = corr_(v1, v2)
print 'corr(v1, v2)=', r
r = corr__(v1, m1, v2, m2, n)
print 'corr(v1, v2)=', r

Message has been deleted

Bryan Catanzaro

unread,
Nov 20, 2012, 1:47:31 PM11/20/12
to copperhe...@googlegroups.com
Hi Richard -
I'm glad you managed to make your code compile. To your other questions:
We don't recommend using the @cutype decorator in your code - it
exists for internal use and shouldn't be necessary in your code.
Otherwise, your code looks fine. =)

- bryan

On Mon, Nov 19, 2012 at 5:23 PM, Richard Gomes <rgome...@gmail.com> wrote:
> I managed to make it compile. But I'd like to hear your thoughts about the
> code.
>
> Thanks

Richard Gomes

unread,
Nov 20, 2012, 8:32:36 PM11/20/12
to copperhe...@googlegroups.com
Hello Bryan,

Does @cutype help on static type checking?

Thanks

Bryan Catanzaro

unread,
Nov 20, 2012, 9:35:46 PM11/20/12
to copperhe...@googlegroups.com
At the moment, no, it doesn't help with static type checking. Perhaps it could be used for that, but as I said it's mostly intended for internal use.

- bryan

Richard Gomes

unread,
Nov 21, 2012, 6:17:59 PM11/21/12
to copperhe...@googlegroups.com
Hi Bryan,

Is there documentation or a paper or something which clarifies how @cutype is employed?
Maybe examining sources?

Thanks a lot for your answers :)
Reply all
Reply to author
Forward
0 new messages