How does concat works using ipython? - ipython

Can somebody explain me the code below?
Asume n_a and n_x are known.
Questions I have for the below code are:
for the second concat, why is there no , between : and n_a
and idem for the third contact (no , between n_a and :)?
What is the difference between : n_a and n_a : ?
What is the difference in : before and after the , (in both concats ?)
concat = np.zeros((n_a + n_x, m))
concat[: n_a, :] = a_prev
concat[n_a :, :] = xt

Related

this python program guess a random number, how to calculate the average numberguesses?

import random
i went to add another function to calculate the number guesses
def guess(x):
randomNumb = random.randint(1, x)
guess = 0
while guess != randomNumb :
guess = int(input(f'entre number between 1 and {x} : '))
print(guess)
if guess < randomNumb :
print('guess is low')
elif guess > randomNumb :
print('guess is high')
print(f'guess is right {randomNumb}')
guess(100)

How does wavg work in depth using a vectorised approach?

The objective of the snippet below is to evaluate weighted mid for n levels of an order book. The code snippet is from the book Machine Learning and Big Data with kdb+/q (2020 Wiley).
n:10;
quote: ([] sym: n?`A`B; time: asc n?0t; bid1: n?10f; bidSize1: n?100 );
update bid2: 0 | bid1 - .1 * n ? 10, bidSize2: n?100, ask1: bid1 + .2 * n ? 10, askSize1: n?100 from quote;
update ask2: ask1 + .1 * n ? 10, askSize2: n?100 from `quote;
select sym,time, wmid: ( bidSize1; bidSize2; askSize1; askSize2 ) wavg (bid1; bid2; ask1; ask2) from quote
I would like to understand a generic rule for how the wavg method works in-depth for lists of vectors. Could you please help me? Appreciate your help.
There are docs here on wavg https://code.kx.com/q/ref/avg/#wavg.
From these docs we can see that calling wavg is the equivalent to the function {(sum x*y)%sum x}
Using your example:
q)res1:select sym,time, wmid: ( bidSize1; bidSize2; askSize1; askSize2 ) wavg (bid1; bid2; ask1; ask2) from quote;
q)res2:select sym,time, wmid:{(sum x*y)%sum x} [( bidSize1; bidSize2; askSize1; askSize2 );(bid1; bid2; ask1; ask2)] from quote;
q)res1 ~ res2
1b
So in the case of your example we multiply bidSize1bid, bidSize2bid2, etc. , sum this result, then divide by the sums of our sizes e.g. (bidSize1[0]+ bidSize2[0] + askSize1[0] + askSize2[0];(bidSize1[1]+ bidSize2[1] + askSize1[1] + askSize2[1]; etc...)
I'm not sure if there is a more a general way of describing this but the above may help understand the nuts and bolts of what's going on

Improve PySpark implementation for finding connected components in a graph

I am currently working on the implementation of this paper describing Map Reduce Algorithm to fing connected component : https://www.cse.unr.edu/~hkardes/pdfs/ccf.pdf
As a beginner in Big Data world , I started the implementation of CCF-Iterate (w. secondary sorting) algorithm with a small graph : 6 edges and 8 nodes. I'm running this code with the free version of Databricks.
It takes 1 minute to give a result. That seems too long for a such small example . How can I reduce this time ? What kind of optimization is possible? Any advice would be really apreciated.
The idea is to test this algo for large graphs
PySpark code:
graph = sc.parallelize([ (2,3),(1,2), (2,4), (3,5), (6,7),(7,8)])
counter_new_pair = sc.accumulator(1)
while (counter_new_pair.value > 0):
counter_new_pair = sc.accumulator(0)
#CCF Iterate Sorting
mapping_1 = graph.map(lambda x : (x[0], x[1]))
mapping_2 = graph.map(lambda x : (x[1], x[0]))
fusion = mapping_1.union(mapping_2)
fusion = fusion.groupByKey().map(lambda x : (x[0], list(x[1])))
fusion = fusion.map(lambda x : (x[0], sorted(x[1])))
values = fusion.filter(lambda x : x[1][0] < x[0])
key_min_value = values.map(lambda x : (x[0], x[1][0]))
values = values.map(lambda x : (x[1][0], x[1][1:]))
values = values.filter(lambda x : len(x[1]) != 0)
values = values.flatMap(lambda x : [(val, x[0]) for val in x[1]])
values.foreach(lambda x: counter_new_pair.add(1))
joined = values.union(key_min_value)
# CCF Dedup
mapping = joined.map(lambda x : ((x[0], x[1]), None))
graph = mapping.groupByKey().map(lambda x : (x[0][0], x[0][1]))
Thanks

SSP Algorithm minimal subset of length k

Suppose S is a set with t elements modulo n. There are indeed, 2^t subsets of any length. Illustrate a PARI/GP program which finds the smallest subset U (in terms of length) of distinct elements such that the sum of all elements in U is 0 modulo n. It is easy to write a program which searches via brute force, but brute force is infeasible as t and n get larger, so would appreciate help writing a program which doesn't use brute force to solve this instance of the subset sum problem.
Dynamic Approach:
def isSubsetSum(st, n, sm) :
# The value of subset[i][j] will be
# true if there is a subset of
# set[0..j-1] with sum equal to i
subset=[[True] * (sm+1)] * (n+1)
# If sum is 0, then answer is true
for i in range(0, n+1) :
subset[i][0] = True
# If sum is not 0 and set is empty,
# then answer is false
for i in range(1, sm + 1) :
subset[0][i] = False
# Fill the subset table in botton
# up manner
for i in range(1, n+1) :
for j in range(1, sm+1) :
if(j < st[i-1]) :
subset[i][j] = subset[i-1][j]
if (j >= st[i-1]) :
subset[i][j] = subset[i-1][j] or subset[i - 1][j-st[i-1]]
"""uncomment this code to print table
for i in range(0,n+1) :
for j in range(0,sm+1) :
print(subset[i][j],end="")
print(" ")"""
return subset[n][sm];
I got this code from here I don't know weather it seems to work.
function getSummingItems(a,t){
return a.reduce((h,n) => Object.keys(h)
.reduceRight((m,k) => +k+n <= t ? (m[+k+n] = m[+k+n] ? m[+k+n].concat(m[k].map(sa => sa.concat(n)))
: m[k].map(sa => sa.concat(n)),m)
: m, h), {0:[[]]})[t];
}
var arr = Array(20).fill().map((_,i) => i+1), // [1,2,..,20]
tgt = 42,
res = [];
console.time("test");
res = getSummingItems(arr,tgt);
console.timeEnd("test");
console.log("found",res.length,"subsequences summing to",tgt);
console.log(JSON.stringify(res));

Does anybody know how to find the dual of the following (Boolean Algebra)? p*(q+r')

Does anybody know how to find the dual of the following (Boolean Algebra)?
p*(q+r')
Where * stands for AND, + stands for OR, and ' stands for NOT.
I have the following, but it is incorrect:
p + (qr')
not a and not b => not (a or b)
Here a = p', b = (q+r')' = (q'r). Hence p*(q+r') = (p'+q'r)'