Write a script named char_to_bin.sh
that takes a single commandline input argument and returns that argument encoded to binary. For example:
>> ./char_to_bin.sh "a"
1100001
size(mat,dim)
- Prints the size of a particular dimension of a matrixlength(arr)
- Output the scalar size of an arrayones(x,y)
- Create a vector/matrix of all oneszeros(x,y)
- Create a vector/matrix of all zerosissparse(mat)
- Returns 1 if the storage class is sparsediag(A), diag(v)
- Returns a column vector of the diagonal elements of A
or a square matrix with the elements of v
as the diagonal elements (zeros elsewhere).sparse(A), sparse(m,n)
- Creates a sparse matrix from the matrix A
or an m
by n
sparse matrix of all zeros.find(X>2), find(X>2)=0
](http://www.import numpy | |
import math | |
import random | |
class Diffusion(object): | |
def __init__(self, a_true, prob_model): | |
N = numpy.size(a_true, 0) | |
assert a_true.shape == (N,N), a_true.shape | |
seed_node_index = math.floor(random.random() * N) |
"Premature optimization is the root of all evil"
-- DonaldKnuth
When we're coding we have to make tradeoffs between performance and readability. If you're making a decision where readability really suffers you should definitely consider whether or not your optimization is premature. Below we've attempted to identify a number of cases that are "readability-neutral" and improve performance.
def get_descendants(node, A, descendants=None): | |
if not descendants: | |
descendants = [] | |
descendants.append(node) | |
children = A.getrow(node).indices | |
for child in children: | |
descendants += get_descendants(child, A) |
def get_descendants(node, A): | |
unexplored = deque([node]) | |
descendants = set() | |
while unexplored: | |
node = unexplored.popleft() | |
descendants.add(node) | |
children = A.getrow(node).indices | |
for child in children: | |
unexplored.append(child) |
def get_descendants(node, A): | |
unexplored = deque([node]) | |
while unexplored: | |
node = unexplored.popleft() | |
yield node | |
children = A.getrow(node).indices | |
for child in children: | |
unexplored.append(child) |
class Node(object): | |
def __init__(self, name): | |
self.name = name | |
self.children = [] | |
def add_child(self, child): | |
self.children.append(child) | |
def __repr__(self): | |
return '<{0}>'.format(self.name) |
graph = [] | |
for name in xrange(6): | |
graph.append(Node(name)) | |
graph[0].add_child(graph[1]) | |
graph[0].add_child(graph[2]) | |
graph[2].add_child(graph[3]) | |
graph[2].add_child(graph[4]) | |
graph[4].add_child(graph[5]) |
def get_descendants(node): | |
unexplored = deque([node]) | |
while unexplored: | |
node = unexplored.popleft() | |
yield node | |
children = node.children | |
for child in children: | |
unexplored.append(child) |