Skip to main content
added 24 characters in body
Source Link
Soudipta Dutta
  • 2.1k
  • 1
  • 16
  • 11

Use : np.einsum

import numpy as np

arr = np.arange(1, 17)
 
# Reshapegr_size the= array8 to
half_size split= intogr_size groups// of2 8 

reshaped = arr.reshape(-1, 8)
print('reshaped :'gr_size)
print(reshaped ) 
'''
reshaped :
[[ 1  2  3  4  5  6  7  8]
 [ 9 10 11 12 13 14 15 16]]
'''
 
aa = np.einsum('ij -> ji',reshaped[:,:4]half_size]).T.reshape(-1) 

bb = np.einsum('ij -> ji',reshaped[:,4half_size:]).T.reshape(-1)

print("aa'aa ->">', aa)#[ 1  2  3  4  9 10 11 12]
print("bb'bb ->">', bb)#[ 5  6  7  8 13 14 15 16]
'''
aa -> [ 1  2  3  4  9 10 11 12]
bb -> [ 5  6  7  8 13 14 15 16]
 
'''

Generalized Solution using np.einsum

import numpy as np

def split_and_reshape(arr, group_size):
    # Ensure the array can be reshaped into the desired format
    assert len(arr) % group_size == 0, "Array length must be divisible by the group size"
    
    # Reshape the array into groups of group_size
    reshaped = arr.reshape(-1, group_size)
    
    # Calculate half of the group size for splitting
    half_size = group_size // 2
    
    # Apply einsum and reshape
    a = np.einsum('ij -> ji', reshaped[:, :half_size]).T.reshape(-1)
    b = np.einsum('ij -> ji', reshaped[:, half_size:]).T.reshape(-1)
    
    return a, b


arr = np.arange(1, 17)
a, b = split_and_reshape(arr, 8)

print("a ->", a)
print("b ->", b)
'''
a -> [ 1  2  3  4  9 10 11 12]
b -> [ 5  6  7  8 13 14 15 16]
'''

Use : np.einsum

import numpy as np

arr = np.arange(1, 17)
 
# Reshape the array to split into groups of 8
reshaped = arr.reshape(-1, 8)
print('reshaped :')
print(reshaped )
'''
reshaped :
[[ 1  2  3  4  5  6  7  8]
 [ 9 10 11 12 13 14 15 16]]
'''
 
aa = np.einsum('ij -> ji',reshaped[:,:4]).T.reshape(-1)
bb = np.einsum('ij -> ji',reshaped[:,4:]).T.reshape(-1)

print("aa ->", aa)
print("bb ->", bb)
'''
aa -> [ 1  2  3  4  9 10 11 12]
bb -> [ 5  6  7  8 13 14 15 16]
 
'''

Generalized Solution using np.einsum

import numpy as np

def split_and_reshape(arr, group_size):
    # Ensure the array can be reshaped into the desired format
    assert len(arr) % group_size == 0, "Array length must be divisible by the group size"
    
    # Reshape the array into groups of group_size
    reshaped = arr.reshape(-1, group_size)
    
    # Calculate half of the group size for splitting
    half_size = group_size // 2
    
    # Apply einsum and reshape
    a = np.einsum('ij -> ji', reshaped[:, :half_size]).T.reshape(-1)
    b = np.einsum('ij -> ji', reshaped[:, half_size:]).T.reshape(-1)
    
    return a, b


arr = np.arange(1, 17)
a, b = split_and_reshape(arr, 8)

print("a ->", a)
print("b ->", b)
'''
a -> [ 1  2  3  4  9 10 11 12]
b -> [ 5  6  7  8 13 14 15 16]
'''

Use : np.einsum

import numpy as np

arr = np.arange(1, 17)
gr_size = 8 
half_size = gr_size // 2  

reshaped = arr.reshape(-1,gr_size)
print(reshaped) 
'''
[[ 1  2  3  4  5  6  7  8]
 [ 9 10 11 12 13 14 15 16]]
'''
aa = np.einsum('ij -> ji',reshaped[:,:half_size]).T.reshape(-1) 

bb = np.einsum('ij -> ji',reshaped[:,half_size:]).T.reshape(-1)

print('aa ->',aa)#[ 1  2  3  4  9 10 11 12]
print('bb ->',bb)#[ 5  6  7  8 13 14 15 16]
'''
aa -> [ 1  2  3  4  9 10 11 12]
bb -> [ 5  6  7  8 13 14 15 16]
'''

Generalized Solution using np.einsum

import numpy as np

def split_and_reshape(arr, group_size):
    # Ensure the array can be reshaped into the desired format
    assert len(arr) % group_size == 0, "Array length must be divisible by the group size"
    
    # Reshape the array into groups of group_size
    reshaped = arr.reshape(-1, group_size)
    
    # Calculate half of the group size for splitting
    half_size = group_size // 2
    
    # Apply einsum and reshape
    a = np.einsum('ij -> ji', reshaped[:, :half_size]).T.reshape(-1)
    b = np.einsum('ij -> ji', reshaped[:, half_size:]).T.reshape(-1)
    
    return a, b


arr = np.arange(1, 17)
a, b = split_and_reshape(arr, 8)

print("a ->", a)
print("b ->", b)
'''
a -> [ 1  2  3  4  9 10 11 12]
b -> [ 5  6  7  8 13 14 15 16]
'''
deleted 306 characters in body
Source Link
Soudipta Dutta
  • 2.1k
  • 1
  • 16
  • 11

Use : np.einsum

import numpy as np

arr = np.arange(1, 17)

# Reshape the array to split into groups of 8
reshaped = arr.reshape(-1, 8)
print('reshaped :')
print(reshaped )
'''
reshaped :
[[ 1  2  3  4  5  6  7  8]
 [ 9 10 11 12 13 14 15 16]]
'''

aa = np.einsum('ij -> ji',reshaped[:,:4]).T.reshape(-1)
bb = np.einsum('ij -> ji',reshaped[:,4:]).T.reshape(-1)

print("aa ->", aa)
print("bb ->", bb)
'''
aa -> [ 1  2  3  4  9 10 11 12]
bb -> [ 5  6  7  8 13 14 15 16]

'''

Generalized Solution using np.einsum

import numpy as np

def split_and_reshape(arr, group_size):
    # Ensure the array can be reshaped into the desired format
    assert len(arr) % group_size == 0, "Array length must be divisible by the group size"
    
    # Reshape the array into groups of group_size
    reshaped = arr.reshape(-1, group_size)
    
    # Calculate half of the group size for splitting
    half_size = group_size // 2
    
    # Apply einsum and reshape
    a = np.einsum('ij -> ji', reshaped[:, :half_size]).T.reshape(-1)
    b = np.einsum('ij -> ji', reshaped[:, half_size:]).T.reshape(-1)
    
    return a, b


arr = np.arange(1, 17)
a, b = split_and_reshape(arr, 8)

print("a ->", a)
print("b ->", b)
'''
a -> [ 1  2  3  4  9 10 11 12]
b -> [ 5  6  7  8 13 14 15 16]
'''

Use : np.einsum

import numpy as np

arr = np.arange(1, 17)

# Reshape the array to split into groups of 8
reshaped = arr.reshape(-1, 8)
print('reshaped :')
print(reshaped )
'''
reshaped :
[[ 1  2  3  4  5  6  7  8]
 [ 9 10 11 12 13 14 15 16]]
'''

aa = np.einsum('ij -> ji',reshaped[:,:4]).T.reshape(-1)
bb = np.einsum('ij -> ji',reshaped[:,4:]).T.reshape(-1)

print("aa ->", aa)
print("bb ->", bb)
'''
aa -> [ 1  2  3  4  9 10 11 12]
bb -> [ 5  6  7  8 13 14 15 16]

'''

Use : np.einsum

import numpy as np

arr = np.arange(1, 17)

# Reshape the array to split into groups of 8
reshaped = arr.reshape(-1, 8)
print('reshaped :')
print(reshaped )
'''
reshaped :
[[ 1  2  3  4  5  6  7  8]
 [ 9 10 11 12 13 14 15 16]]
'''

aa = np.einsum('ij -> ji',reshaped[:,:4]).T.reshape(-1)
bb = np.einsum('ij -> ji',reshaped[:,4:]).T.reshape(-1)

print("aa ->", aa)
print("bb ->", bb)
'''
aa -> [ 1  2  3  4  9 10 11 12]
bb -> [ 5  6  7  8 13 14 15 16]

'''

Generalized Solution using np.einsum

import numpy as np

def split_and_reshape(arr, group_size):
    # Ensure the array can be reshaped into the desired format
    assert len(arr) % group_size == 0, "Array length must be divisible by the group size"
    
    # Reshape the array into groups of group_size
    reshaped = arr.reshape(-1, group_size)
    
    # Calculate half of the group size for splitting
    half_size = group_size // 2
    
    # Apply einsum and reshape
    a = np.einsum('ij -> ji', reshaped[:, :half_size]).T.reshape(-1)
    b = np.einsum('ij -> ji', reshaped[:, half_size:]).T.reshape(-1)
    
    return a, b


arr = np.arange(1, 17)
a, b = split_and_reshape(arr, 8)

print("a ->", a)
print("b ->", b)
'''
a -> [ 1  2  3  4  9 10 11 12]
b -> [ 5  6  7  8 13 14 15 16]
'''
deleted 306 characters in body
Source Link
Soudipta Dutta
  • 2.1k
  • 1
  • 16
  • 11

Use : np.einsum

import numpy as np
 
# This creates an array [1, 2, 3, ..., 16]
arr = np.arange(1, 17)  

# Calculate number of groups
# Calculate how many full groups of 8 elements can be formed
groups = len(arr) // 8  

# Reshape the array to split into groups of 8
reshaped = arr[:groups * 8]arr.reshape(groups-1, 8)
print('reshaped :')
print(reshaped )
'''
reshaped :
[[ 1  2  3  4  5  6  7  8]
 [ 9 10 11 12 13 14 15 16]]
'''
# Extract the two groups of 4 elements using indexing
# Extracts the first 4 elements from each group andaa flattens= them
anp.einsum('ij =-> ji',reshaped[:, :4]).ravelT.reshape(-1)  
# Extracts the last 4 elements from each group andbb flattens= them
bnp.einsum('ij =-> ji',reshaped[:, 4:]).ravelT.reshape(-1) 

# Print or use the results as needed
print("a"aa ->", aaa)
print("b"bb ->", bbb)
'''
aaa -> [ 1  2  3  4  9 10 11 12]
bbb -> [ 5  6  7  8 13 14 15 16] 

'''
import numpy as np
 
# This creates an array [1, 2, 3, ..., 16]
arr = np.arange(1, 17)  

# Calculate number of groups
# Calculate how many full groups of 8 elements can be formed
groups = len(arr) // 8  

# Reshape the array to split into groups of 8
reshaped = arr[:groups * 8].reshape(groups, 8)
print(reshaped )
'''
[[ 1  2  3  4  5  6  7  8]
 [ 9 10 11 12 13 14 15 16]]
'''
# Extract the two groups of 4 elements using indexing
# Extracts the first 4 elements from each group and flattens them
a = reshaped[:, :4].ravel()  
# Extracts the last 4 elements from each group and flattens them
b = reshaped[:, 4:].ravel() 

# Print or use the results as needed
print("a ->", a)
print("b ->", b)
'''
a -> [ 1  2  3  4  9 10 11 12]
b -> [ 5  6  7  8 13 14 15 16]
'''

Use : np.einsum

import numpy as np

arr = np.arange(1, 17)

# Reshape the array to split into groups of 8
reshaped = arr.reshape(-1, 8)
print('reshaped :')
print(reshaped )
'''
reshaped :
[[ 1  2  3  4  5  6  7  8]
 [ 9 10 11 12 13 14 15 16]]
'''

aa = np.einsum('ij -> ji',reshaped[:,:4]).T.reshape(-1)
bb = np.einsum('ij -> ji',reshaped[:,4:]).T.reshape(-1)

print("aa ->", aa)
print("bb ->", bb)
'''
aa -> [ 1  2  3  4  9 10 11 12]
bb -> [ 5  6  7  8 13 14 15 16] 

'''
Source Link
Soudipta Dutta
  • 2.1k
  • 1
  • 16
  • 11
Loading