For example, there are two 164*90 matrixes and a function.
beta=: 4 : 0
a=. x cor y
b=. sd x
c=. sd y
a*b%c
)
I want to use each row of matrix 1 and each row of matrix 2 into the function. How can I do it at once?
beta=: 4 : 0"1
a=. x cor y
b=. sd x
c=. sd y
a*b%c
)
The "1 after the 0 in the definition above makes your verb rank 1 which means that it would take whatever matrices it is given a row at a time. An alternative would be to call your originally defined verb with rank "1
x beta"1 y
Related
I need to loop through 2 variables and cycle through 1 variable from 2 variables (whichever is bigger) until the range of the 2nd (longest) last.
For example
x = 5 #input by user
y = 8 #input by user
for x_val, y_val in itertools.zip_longest(range(x), range(y), fillvalue='-'):
print(x_val)
print(y_val)
Expected output
0
0
1
1
2
2
3
3
4
4
0
5
1
6
2
7
tried
x = 5
x_cyc = itertools.cycle(range(x))
y = 8
for x_val, y_val in itertools.zip_longest(range(x), x_cyc):
print(x_val)
print(y_val)
but that didn't make much sense.
you dont need zip longest, you create an infinite cycle for the smaller of the two numbers and then normal range for the larger number. this way the min range will be infinite and max range will be the finite range.
You can simply use normal zip to go through them till you reach the end of the non infinite range.
from itertools import cycle
x = 8
y = 5
min_range = cycle(range(min(x, y)))
max_range = range(max(x, y))
for x_val, y_val in zip(min_range, max_range):
print(x_val)
print(y_val)
OUTPUT
0
0
1
1
2
2
3
3
4
4
0
5
1
6
2
7
UPDATE BASED ON COMMENTS
Now the x_val and y_val are bound to the x and y range and the lowest of x or y ints will be cycled in range.
from itertools import cycle
x = 8
y = 5
x_range = range(x)
y_range = range(y)
if x > y:
y_range = cycle(y_range)
elif y > x:
x_range = cycle(x_range)
for x_val, y_val in zip(x_range, y_range):
print(x_val)
print(y_val)
Note that the output will now differ when x is greater than y or when y is greater than x since x will always output first.
OUTPUT x=2, y=3
0
0
1
1
0
2
OUTPUT x=3 y=2
0
0
1
1
2
0
I think I am forgetting a simple formatting button but basically I have data like
x 1
x 2
x 3
x 4
x 5
x 6
x 7
y 8
y 9
y 10
y 11
y 12
y 13
z 14
z 15
z 16
z 17
z 18
z 19
z 20
How would I isolate the first X, Y, and Z cells, giving me the numbers 1, 8, and 14? etc.
So every new piece of text (i.e. A, b, c) it would isolate only the first row that contained the new content.
Consider:
x =. 0 1 2 3 4 1 3 4 99
v =. [ {.~ (>: # i.&1 # (##~. = #\))
v x NB. => 0 1 2 3 4 1
The behavior is correct. But as you can see, v is shamefully verbose. Is there a better solution?
You want the monad ~: (nub sieve):
v =: {.~ 1 + 0 i.~ ~:
x =: 0 1 2 3 4 1 3 4 99
v x
0 1 2 3 4 1
Code review:
Outside code-golf contexts, don't use #\ in place of i.##. It's too cutesy, hard to maintain, and won't be recognized by the special-code optimizer.
Don't assign to the names x, y, u, v, m, or n (except in special circumstances, and always locally in an explicit context).
I have a dataset which has a row for each loan, and a borrower can have multiple loans. The 'Property' flag shows if there is any security behind the loan. I am trying to aggregate this flag on a borrower level, so for each borrower, if one of the Property flags is 'Y', I want to add an additional column where it is 'Y' for each of the borrowers.
The short example below shows what the end result should look like. Any help would be appreciated.
import pandas as pd
data = {'Borrower': [1,2,2,2,3,3,4,5,6,6],
'Loan' : [1,2,3,4,5,6,7,8,9,10],
'Property': ["Y","N","Y","Y","N","Y","N","Y","N","N"],
'Result': ['Y','Y','Y','Y','Y','Y','N','Y','N','N']}
df = pd.DataFrame.from_dict(data)
You can use Transform on Property after groupby Borrower. Because the ASCII code of 'Y' is bigger than 'N' so if there is any property which is 'Y' for a borrower, max(Property) will give 'Y'.
df['Result2'] = df.groupby('Borrower')['Property'].transform(max)
df
Out[202]:
Borrower Loan Property Result Result2
0 1 1 Y Y Y
1 2 2 N Y Y
2 2 3 Y Y Y
3 2 4 Y Y Y
4 3 5 N Y Y
5 3 6 Y Y Y
6 4 7 N N N
7 5 8 Y Y Y
8 6 9 N N N
9 6 10 N N N
I am trying to solve the below problem. I don't have much knowledge in Affine transformations. Could someone help me answer this question:
Find a 3x3 matrix representing a 2D affine transformation of homogeneous coordinates (i.e. every point [x,y] is represented as a column vector [x, y, 1]), which transforms a square [0,0],[1,0],[1,1],[0,1] into a parallelogram [0,1],[1,1],[2,2],[1,2].
Things I spotted about this question
1) You need to understand homogeneous co-ordinates
2) You need to know the difference between row and column major - read here
3) You need to know the basic affine transformations - rotate, scale/shear and translate and how to represent them in a matrix - read this page
Interestingly, I think the answer only needs a translate and a shear ( no rotation ).
Looking at the source and dest points, it looks like all dest points are translated +1 in y and sheared by 1 in X ( to give the parallelogram, probably best to draw it out to see what I mean )
So start with a 3 * 3 identity matrix which is
1 0 0
0 1 0
0 0 1
The shear will be
1 1 0
0 1 0
0 0 1
The translate will be
1 0 0
0 1 1
0 0 1
So putting it all together should be
1 1 0
0 1 1
0 0 1
I don't normally use column major so probably worth double checking!
Hope that helps
An affine transformation is a transformation of the form x ⟼ Ax + b, where x and b are vectors, and A is a square matrix. Geometrically, affine transformations map parallelograms to parallelograms and preserve relative distances along lines.
To solve a problem like this, we first note that for the origin, we have 0 ⟼ A0 + b = b. Since the problem tells us that [0,0] ⟼ [0,1], we know that b = [0,1].
Next we recall from linear algebra that multiplying a matrix by the standard basis vectors [0,1] and [1,0] simply extracts the first and second columns of the matrix, respectively:
[a b] [1] = [a], [a b] [0] = [b].
[c d] [0] [c] [c d] [1] [d]
We are given that [1,0] ⟼ [1,1] and [0,1] ⟼ [1,2]. From this we obtain
[1,1] = A[1,0] + b = [a,c] + [0,1] ⟹ [a,c] = [1,0],
[1,2] = A[0,1] + b = [b,d] + [0,1] ⟹ [b,d] = [1,1].
This gives us our affine transformation
Ax + b = [1 1] x + [0].
[0 1] [1]
Homogeneous coordinates are a trick which let us write affine transformations as matrices, just with one extra coordinate that is always set to 1. The matrix formula is
[A b] [x] = [Ax+b].
[0 1] [1] [ 1]
Here A is actually a 2×2 matrix, while b and x are 2-vectors, and the 0 in the bottom left is really [0 0]. So overall, we are dealing with a 3×3 matrix and 3-vectors.
So our solution is
[1 1 0]
[0 1 1],
[0 0 1]
and for good measure we check that it works properly for the final point:
[1 1 0] [1] [2]
[0 1 1] [1] = [2].
[0 0 1] [1] [1]
You'll have read the Wikipedia page on the subject, of course.
Once upon an aeon or so ago, I read Foley and van Dam in one of the predecessor versions (this would have been 1983 or 1984), and it covered techniques for manipulating 2D and 3D coordinates with augmented matrices and vectors as described in the question. However, enough time has lapsed since then that I've forgotten all the details (and no longer have the book--too many moves of house). There was also a book by Newman and Sproul, I seem to remember.
A = [ a b c ] B = [ 0 1 1 0 ] C = [ 0 1 2 1 ]
[ d e f ] [ 0 0 1 1 ] [ 1 1 2 2 ]
[ g h 1 ] [ 1 1 1 1 ] [ 1 1 1 1 ]
The columns of B represent the corners of the square; the columns of C represent the corners of the parallelogram; and the matrix equation A x B = C has to be solved. IIRC, the matrix A has a 1 in the bottom right corner; it is possible that the values c, f, g, and h also have presecribed values (they'd probably be zeroes). The non-zero values apply a linear (affine) transform, scaling, shearing and rotating the input shape.
You'd need to look for similar information in a text book. Or in the Wiki page - I didn't look hard at it (the information above is working from ancient memory).
I just wanted to point out that four points over constrain a 2D affine transformation. In the comment of Jonathan Leffler, you can see this from the fact that you would need to invert a non-square matrix. So, either choose three of the points or set up a least-squares system. The over-constrained, least-squares solution could be solved with the following matrices
A = [ a b c ] B = [ 0 1 1 0 ] C = [ 0 1 2 1 ]
[ d e f ] [ 0 0 1 1 ] [ 1 1 2 2 ]
[ g h 1 ] [ 1 1 1 1 ] [ 1 1 1 1 ]
so that solving using the normal equations gives
A B = C
(A B)^T = B^T A^T = C^T
B B^T A^T = B C^T
A^T = (B B^T)^-1 B C^T
undoing that transpose gives
A = ((B B^T)^-1 B C^T)^T