Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I'm playing with graphs and python, and I'm trying to test some code on all the possible square matrix that represent an adjacency matrix (i.e. matrix with 0 and 1).
We know that there are 2^{n^2} possible matrix of nxn.
What's the best code for generating all the possible n x n binary matrix in python?
I think you can more efficiently compute your results by using mathematical operations with numpy rather than string operations. Try:
shift = np.arange(n*n).reshape(n, n)
for j in range(2**(n*n)):
yield j >> shift & 1
You might be able to use numpy to parallelize the j loop as well, but that might use a lot more memory than the current generator version.
Since I could't find anywhere the solution, and I think it could be helpful to spare some minutes to other people..
def generateAllBinaryMatrix(n):
G = np.zeros([n,n])
cordx=[]
cordy=[]
for x in range(0,n):
for y in range(0,n):
cordx.append(x)
cordy.append(y)
cx=np.array(cordx)
cy=np.array(cordy)
indices=(cx,cy)
print indices
raw_input()
for j in range(0,2**(indices[0].size)):
G[indices] = [1 if digit=='1' else 0 for digit in bin(j)[2:].zfill(indices[0].size)]
yield (G)
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
Improve this question
I was trying to create this [80000, 104000, 135000...] list in Python. Its the value, starting at 80,000 multiplied by 1.3 each time I want
What i've tried:
a = [num*1.5 for num in ??? if num>=80000] #???--> i've tried range(10)
I should be able to do this but I can't find any solutions rn..
I must use list-comprehensions, if possible.
Some help would be nice, thank you!
There is a very basic mathematical operation that represents multiplying by the same value many time: power.
a = [80000 * (1.3**n) for n in range(100)]
You could write your own generator then use that in conjunction with a list comprehension.
def numgen(start, factor, limit):
for _ in range(limit):
yield int(start)
start *= factor
mylist = [value for value in numgen(80_000, 1.3, 10)]
print(mylist)
Output:
[80000, 104000, 135200, 175760, 228488, 297034, 386144, 501988, 652584, 848359]
import numpy as np
print(80000 * 1.3**np.arange(3))
# [ 80000. 104000. 135200.]
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
There has been a solution to find the possible combination of numbers to reach a given target number. However, I have a different situation below, where a,b, and c are product types and I like to find the combination of sum products of a,b and c to reach the target total.
a = 50sqft
b = 70sqft
c = 100sqft
Total = 5000sqft
I like to find all possible combinations of numbers (integer solution) of a,b,c to get to 5000, and how can I create a python function for that?
Results :
(100a,0b,0c)=5000
(23a,5b,8c)=5000
...
...
Thanks in advance!!
I got a solution :
a=50
b=70
c=100
for i in range(101): # This si 101 here to give 100a=5000
for j in range(100):
for k in range(100):
if i*a + j*b + k*c == 5000:
print('({}a,{}b,{}c)=5000'.format(i,j,k))
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am trying to implement this summation, any help, please?
enter image description here
Considering that x is a list of numbers, this can be written as following:
H = '+'.join([f'x{i}*x{i+1}' for i in range(1, 21)])
>>>print(H)
'x1*x2+x2*x3+x3*x4+x4*x5+x5*x6+x6*x7+x7*x8+x8*x9+x9*x10+x10*x11+x11*x12+x12*x13+x13*x14+x14*x15+x15*x16+x16*x17+x17*x18+x18*x19+x19*x20+x20*x21'
You need a computer algebra system (CAS). There are a number of software packages in Python that implements CAS, for example, using sympy:
from sympy import *
i = symbols('i')
x = IndexedBase('x')
H = Sum(x[i] * x[i-1], (i, 1, 20))
This will produce H, a symbolic expression the represents the equation in your image.
Or, you can use summation instead to evaluate the Sum into a series of additions:
H_evaluated = summation(x[i] * x[i-1], (i, 1, 20))
or call the doit() function:
H_evaluated == H.doit()
You can try sympy online on https://live.sympy.org/ to play with sympy without installing it locally. Try this equation live
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
For generating a probability density function of some cases, maybe 1 million observations are considered. When I work with numpy array, I was encountered by size limit 32.
Is it too few ?
In this case, how can we store more than 32 elements without distributing the elements into different columns and maybe arrays in arrays ?
import numpy
my_list = []
for i in range(0, 100):
my_list.append(i)
np_arr = numpy.ndarray(np_arr) # ValueError: sequence too large; cannot be greater than 32
When you create an array with numpy.ndarray, the first argument is the shape of the array. Interpreting that list as a shape would indeed give a huge array. If you just want to turn the list into an array, you want numpy.array:
import numpy
my_list = []
for i in range(0, 100):
my_list.append(i)
np_arr = numpy.array(my_list)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am trying to randomly generate an array of integers of range between -10^7 to 10^7 such that there exists a sub-array with a sum of zero.
For example : 7 6 4 8 -4 -8
1<=Size of array<=10^5
I am able to do it using brute-force but it is taking a lot of time as I have to generate very big such arrays. Is there any efficient way of achieving this using python or c++?
Edit: Actually I want to generate a lot of such arrays with different scenarios. I am generating these arrays as testcases for a problem to determine whether any given array of positive and negative integers contain a sub-array with zero sum.
Tried brute force code:
import random
N = random.randint(10,100)
mylist = []
for i in xrange(1,N):
mylist.append(random.randint(-100,100))
list2 = [1,-1]
mylist = mylist[1:N-2] + list2 + mylist[N-2:N]
print mylist
So I have to manually tweak it a lot.
Thanks!
The answer depends on how random you want your array to be. Like some of the commenters mentioned, you can always include a zero and are thus guaranteed to have a subarray with sum of zero. I thought it would be helpful to your eventual solution, so I want to mention that you can check if an array has a subarray with sum of zero in O(N^2) time.
def array_has_zerosubarray( A ):
for _begin in xrange(0,len(A)-1):
for _end in xrange(_begin+1,len(A)):
sum = 0
for ai in range(_begin,_end):
sum = sum + A[ai]
if sum==0:
return True
return False