Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python matrix condensed to square

import math

def calc_row_idx(k, n):
    return int(math.ceil((1/2.) * (- (-8*k + 4 *n**2 -4*n - 7)**0.5 + 2*n -1) - 1))

def elem_in_i_rows(i, n):
    return i * (n - 1 - i) + (i*(i + 1))//2

def calc_col_idx(k, i, n):
    return int(n - elem_in_i_rows(i + 1, n) + k)

def condensed_to_square(k, n):
    i = calc_row_idx(k, n)
    j = calc_col_idx(k, i, n)
    return i, j
Comment

python matrix condensed to square

def square_to_condensed(i, j, n):
    assert i != j, "no diagonal elements in condensed matrix"
    if i < j:
        i, j = j, i
    return n*j - j*(j+1)//2 + i - 1 - j
Comment

python matrix condensed to square

>>> square_to_condensed(1, 2, len(points))
3
>>> dist_condensed[3]
4.4721359549995796
>>> dist[1,2]
4.4721359549995796
Comment

PREVIOUS NEXT
Code Example
Python :: falcon 900 price 
Python :: ec2 ssh terminal hangs after sometime 
Python :: combining sparse class 
Python :: How to put a header title per dataframe after concatenate using pandas in python 
Python :: exec inside def is not working in python 
Python :: what is horse riding sport name 
Python :: sklearn recognising sentences 
Python :: use of numpy matrix in tkinter python 3 
Python :: create series with number intervals 
Python :: pandas resample fill missing values 
Python :: sns linear regression 
Python :: if short for python 
Python :: python generate fibonacci series 
Python :: Parallel run of a function with multiple arguments partial map pool 
Python :: add vertical line to horizontal graph 
Python :: list in pythom 
Python :: python invalid syntax for no reason 
Python :: import sys locate python = sys.exec_prefix print(locate python) 
Python :: how to make a series in python alternating between + and - 
Python :: off to obj python 
Python :: Local to ISO 8601 without microsecond: 
Python :: chain lists 
Python :: schema json in oython 
Python :: flask crud generator 
Python :: python sorted vs sort 
Python :: nbt python 
Python :: html in nested structure 
Python :: Math Module fabs() Function in python 
Python :: view(-1 1) pytorch 
Python :: how to move mouse by detected face and eye using opencv 
ADD CONTENT
Topic
Content
Source link
Name
5+8 =