[PYTHON MATRIX-SIG] Another problem?

Rob.Hooft@embl-heidelberg.de Rob.Hooft@embl-heidelberg.de
Fri, 16 Aug 1996 12:14:05 +0200 (MET DST)


While hacking some more on my "code-cracker" :-) script, I found what I think
is another problem:

   nu[101]NumPy% python
   Python 1.4b2 (Aug 12 1996) [C]
   Copyright 1991-1996 Stichting Mathematisch Centrum, Amsterdam
   >>> from Numeric import *
   >>> nonzero([0,2,0,4])
   1 1 3 3 3 3
   >>> 

from the description I read nonzero should return [1,3] here. It looks
like a mixture with repeat... 

Furthermore, I did some more work on the script, got it working, but
the results are very disappointing:

  Can divide by 2
  Can divide by 29
  factor took  10.577 seconds
  Can divide by 2
  Can divide by 29
  afactor took   7.916 seconds

It gets WORSE if I increase the size of the problem. Profiling (run
appended) changes the ratio between conventional and numeric
dramatically, so that is rather useless in this case.

Can anyone help me to make optimal use of the arrays here? The script
is of no serious use whatsoever (it may make a simple demo), but my
understanding of Numeric might be boosted....

Rob.
----------------------------------------------------------
from Numeric import *
import math

def sieve(max):
	numbers=range(max+1)
	size=int(math.sqrt(max))
	if size<5:
		trials=[2,3]
	else:
		trials=sieve(size)
	for i in trials:
		try:
			j=i*i
			while 1:
				numbers[j]=0
				j=j+i
		except IndexError:
			pass
	return filter(lambda x:x>1,numbers)

def asieve(max,atype='l'):
	numbers=array([1],atype)
	numbers=resize(numbers,[max+1])
	# one is not prime
        numbers[1]=0
	size=int(math.sqrt(max))
	if size<5:
		trials=[2,3]
	else:
		trials=asieve(size)
	#print "We need to try all in "+`trials`
	for i in trials:
		#print "Filtering %d by %d"%(max,i)
		cond=concatenate((array([1],atype),zeros([i-1],atype)))
		#print cond
		cond=resize(cond,[max+1])
		#print cond
		cond[i]=0
		#print cond
		#print numbers
		numbers=where(cond,0,numbers)
		#print numbers
	return nonzero(numbers)

def factor(num,upto=1000000):
	for p in sieve(upto):
		if num%p==0:
			print "Can divide by %d"%p

def afactor(num,upto=1000000):
	for p in asieve(upto):
		if num%p==0:
			print "Can divide by %d"%p

import timing
timing.start()
factor(129753308,upto=99999)
timing.finish()
print "factor took %7.3f seconds"%(timing.micro()/1.e6)
timing.start()
afactor(129753308,upto=99999)
timing.finish()
print "afactor took %7.3f seconds"%(timing.micro()/1.e6)
import profile

#profile.run("factor(129753308,upto=99999)")
#profile.run("afactor(129753308,upto=99999)")

----------------------------------------------------------
nu[120]NumPy% python ~/x.py
Can divide by 2
Can divide by 29
         100341 function calls (100339 primitive calls) in 284.433 CPU seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1  216.867  216.867  284.433  284.433 profile:0(factor(129753308,upto=99999))
        0    0.000             0.000          profile:0(profiler)
        1    0.000    0.000   67.567   67.567 python:0(14222.C.1)
   100335   29.233    0.000   29.233    0.000 x.py:19(<lambda>)
      3/1   38.133   12.711   67.367   67.367 x.py:4(sieve)
        1    0.200    0.200   67.567   67.567 x.py:45(factor)


Can divide by 2
Can divide by 29
         397 function calls (395 primitive calls) in 15.233 CPU seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
       77    0.033    0.000    0.033    0.000 Numeric.py:152(ravel)
        3    0.033    0.011    0.233    0.078 Numeric.py:158(nonzero)
        3    0.150    0.050    0.200    0.067 Numeric.py:16(arrayRange)
       74    3.767    0.051    3.767    0.051 Numeric.py:171(where)
        3    0.050    0.017    0.050    0.017 Numeric.py:192(ones)
      151    2.133    0.014    2.133    0.014 Numeric.py:43(concatenate)
       77    0.250    0.003    2.383    0.031 Numeric.py:81(resize)
        3    0.000    0.000    0.000    0.000 Precision.py:34(Integer)
        1    8.483    8.483   15.233   15.233 profile:0(afactor(129753308,upto=99999))
        0    0.000             0.000          profile:0(profiler)
        1    0.000    0.000    6.750    6.750 python:0(14222.C.2)
      3/1    0.117    0.039    6.533    6.533 x.py:21(asieve)
        1    0.217    0.217    6.750    6.750 x.py:50(afactor)
-------------------------------------------------------------

-- 
=== Rob.Hooft@EMBL-Heidelberg.DE   http://www.Sander.EMBL-Heidelberg.DE/rob/ ==
==== In need of protein modeling?  http://www.Sander.EMBL-Heidelberg.DE/whatif/
Validation of protein structures?  http://biotech.EMBL-Heidelberg.DE:8400/ ====
== PGPid 0xFA19277D == Use Linux!  Free Software Rules The World! =============

=================
MATRIX-SIG  - SIG on Matrix Math for Python

send messages to: matrix-sig@python.org
administrivia to: matrix-sig-request@python.org
=================