Prime Integer - Add Smallest Value - time limit exceeding python - python-3.x
The program must accept N integers as the input. For each integer value X among the N integers, the program must print the smallest values that can be added to the integer X (possibly zero) so that the integer becomes a prime
Boundary :
1<=N<=2,00,000
2<=Each Int<=10^6
TimeLimit : 100 ms
My Code :
def isPrime(n) :
if (n <= 1) :
return False
if (n <= 3) :
return True
if (n % 2 == 0 or n % 3 == 0) :
return False
i = 5
while(i * i <= n) :
if (n % i == 0 or n % (i + 2) == 0) :
return False
i = i + 6
return True
def summ(g):
for i in range(20):
if isPrime(g+i):
return i
n = int(input())
print(*(summ(i) for i in list(map(int,input().split()))))
Example :
Input :
7
89 54 36 74 44 19 12
Output:
0 5 1 5 3 0 1
My code has no error, and i have used the optimised way to find whether a integer is prime or not but still i get time limit exceed for N == 100,000 Is there any way to reduce the time limit to 100 ms
The worst case scenario is
n=200,000
[169988', '865748', '818394', '160754', '532538', '44700', '607214', '404390', '405894', '954978', '115734', '871022', '762398', '744128', '394638', '981440', '966030', '515540', '859578', '116240', '534858', '263984', '159408', '333930', '310728', '949262', '580734', '40032', '289244', '130052', '921200', '452534', '58632', '689964', '250920', '126224', '845184', '806088', '867734', '662370', '204668', '3728', '193164', '187964', '477260', '365138', '21194', '334760', '46272', '401244', '239934', '670490', '894330', '408770', '766068', '129968', '702828', '354464', '396834', '905340', '586770', '543860', '923388', '197610', '809834', '862344', '194204', '609278', '7874', '411158', '701220', '78780', '525980', '949812', '58700', '829224', '885608', '38630', '507372', '866330', '12918', '292058', '143978', '801290', '97460', '746118', '625610', '48590', '182124', '127952', '545388', '922638', '268050', '801488', '613290', '276278', '387138', '664442', '519588', '418812', '536532', '755570', '283832', '92004', '66890', '724520', '11594', '134742', '542684', '169910', '37022', '689762', '633878', '640968', '277550', '839162', '953150', '742370', '219954', '271058', '846722', '22398', '237930', '637712', '46220', '868740', '9872', '573108', '399720', '114810', '262194', '122100', '104712', '494354', '791804', '279110', '484544', '89460', '797682', '875298', '740522', '218510', '188954', '337794', '375158', '296160', '172604', '871958', '451362', '746414', '757344', '657894', '60734', '485732', '213650', '206300', '716858', '101064', '118820', '185750', '332264', '93288', '363590', '555828', '120284', '814008', '954912', '513698', '795002', '384344', '116960', '866738', '926660', '998528', '170384', '569244', '695504', '77864', '469070', '38784', '47388', '358484', '533738', '966432', '126714', '440822', '964040', '808958', '698388', '157428', '338412', '332208', '976514', '25794', '251540', '399872', '765468', '331142', '454722', '263958', '51614', '694080', '422268', '14754', '661268', '479798', '647402', '198842', '896112', '346562', '72354', '106950', '516234', '313154', '60728', '244640', '345732', '215768', '75980', '349110', '589140', '950690', '476888', '246540', '150908', '291104', '42768', '82470', '751278', '366678', '806904', '741080', '356094', '206828', '850388', '270338', '649218', '10274', '48884', '935814', '978750', '892628', '212678', '163212', '133650', '883974', '665294', '806762', '876824', '8838', '697262', '403434', '114408', '382862', '351438', '969342', '79802', '645000', '113028', '349338', '354150', '384548', '20564', '465644', '721452', '607418', '575250', '940032', '463890', '579282', '862992', '847664', '896634', '828408', '847202', '443940', '879114', '418512', '997202', '690104', '770184', '594750', '851034', '447108', '640650', '271218', '990762', '493920', '594642', '852830', '191562', '794994', '117390', '672902', '978018', '59772', '842450', '153818', '139362', '712302', '437862', '263724', '201894', '575220', '481640', '242010', '466730', '997370', '128450', '174222', '109904', '455970', '564498', '534740', '340802', '358074', '796152', '97022', '630024', '221874', '950738', '730592', '907394', '566988', '580982', '155502', '425520', '908808', '199374', '491498', '50232', '107468', '857322', '728628', '477410', '430290', '643458', '642582', '612714', '705560', '834470', '775344', '55734', '782088', '13800', '432344', '146778', '518412', '450600', '163322', '854958', '30894', '872090', '855734', '304212', '535124', '68438', '734480', '532752', '547038', '366522', '424268', '644364', '152658', '17402', '932750', '465008', '807540', '670730', '205398', '988850', '779900', '231968', '746024', '678612', '319682', '357564', '521982', '363902', '487692', '880730', '340958', '807338', '540612', '38012', '21912', '657500', '836570', '647430', '884294', '470078', '875114', '266354', '721058', '853970', '265872', '795102', '788078', '451580', '169952', '997268', '147648', '185768', '286802', '408744', '459230', '33108', '490004', '27998', '123050', '153702', '151142', '543828', '18798', '498104', '459842', '895314', '119298', '787712', '197424', '918752', '82268', '443732', '839430', '134334', '570392', '16964', '537242', '149112', '500180', '531912', '556944', '942860', '892862', '518690', '330612', '904088', '746678', '310232', '876374', '345908', '889280', '619188', '808994', '658212', '248888', '135572', '55890', '741342', '512538', '55932', '251622', '12228', '687458', '48342', '310364', '345228', '436158', '72720', '634688', '179990', '527942', '939924', '68042', '902502', '295334', '9414', '203354', '758112', '134918', '661482', '948174', '476082', '75330', '94874', '825108', '78008', '833048', '647588', '951278', '857714', '970538', '248628', '141444', '477768', '464814', '514278', '53234', '866714', '455514', '188300', '90620', '206624', '259682', '13760', '77154', '272772', '58604', '358880', '522080', '914240', '644790', '1700', '381570', '955278', '429242', '713192', '745934', '961100', '480584', '352250', '658488', '929808', '224430', '52584', '915974', '147180', '953112', '585792', '212058', '915144', '953502', '756704', '800144', '143138', '96780', '303890', '557282', '999050', '680658', '446934', '304962', '211868', '308928', '42452', '578484', '853320', '671358', '780050', '196908', '983930', '950810', '65124', '489794', '391250', '708284', '351078', '34920', '116868', '397634', '19604', '867620', '547770', '746792', '503624', '245630', '62202', '222008', '207470', '674162', '136538', '130100', '452042', '384488', '432662', '280352', '570192', '905652', '529808', '33410', '482622', '992372', '28278', '196202', '170778', '874772', '80538', '450842', '532452', '164430', '411992', '456960', '32942', '934002', '565248', '973760', '469770', '280298', '21384', '807474', '254520', '235242', '633938', '873404', '687180', '305472', '377844', '110850', '289848', '969444', '812138', '558422', '334932', '630920', '276444', '483140', '951362', '720092', '286814', '503054', '727668', '587520', '307512', '448598', '332952', '7322', '721014', '869900', '278690', '169244', '763494', '358314', '438828', '23628', '241982', '672044', '499854', '4458', '191510', '814328', '728874', '948090', '327344', '957332', '116868', '879170', '442208', '113024', '11312', '446190', '847934', '792108', '307578', '60938', '987660', '26628', '996600', '21882', '580170', '877940', '633470', '388692', '7938', '543608', '806372', '544938', '819150', '439788', '393630', '126962', '662714', '595962', '759180', '76718', '731118', '514890', '125222', '384438', '645684', '268884', '608118', '537374', '500258', '90584', '27774', '195992', '872090', '923712', '24692', '600360', '571854', '863120', '718304', '29790', '107508', '717428', '52070', '609374', '208254', '1230', '92684', '51488', '986114', '798228', '951080', '944310', '582458', '182058', '41958', '488922', '101604', '302010', '459182', '912344', '868874', '1298', '166350', '531638', '219282', '99000', '689412', '809604', '60398', '814008', '211658', '357672', '26628', '874824', '520380', '38288', '105608', '581730', '978474', '972684', '4598', '92762', '2162', '630912', '225242', '663654', '145220', '517824', '999102', '414312', '211502', '833310', '541712', '379724', '902600', '277494', '607038', '329432', '757364', '784628', '25170', '282428', '35324', '932664', '455402', '796260', '540588', '606302', '22434', '128340', '10464', '138684', '34614', '534342', '660120', '271068', '346440', '959370', '178118', '256020', '10772', '657708', '781044', '228678', '311882', '488', '510452', '327444', '875712', '647964', '869438', '121260', '579504', '532920', '938342', '540438', '784548', '260922', '186480', '653820', '163772', '130364', '959490', '810194', '812250', '375252', '168458', '476478', '838694', '257372', '311420', '615068', '961118', '950634', '702018', '408804', '111494', '591752', '467472', '599742', '374118', '509222', '283008', '91250', '362852', '718974', '580734', '755318', '920790', '908582', '72224', '936054', '698670', '892604', '510332', '171618', '688680', '299018', '864290', '426998', '663410', '710372', '521642', '43952', '389798', '753984', '107310', '81902', '915380', '883490', '275912', '70452', '702102', '170958', '479570', '269210', '871160', '158792', '473288', '345432', '758930', '848850', '725910', '557694', '515598', '30638', '571370', '189068', '294704', '793548', '745118', '832478', '615722', '156362', '854364', '688160', '363152', '631938', '236682', '460464', '36068', '269982', '634344', '348210', '249974', '324774', '705534', '79394', '122850', '694082', '346878', '704688', '880514', '77558', '647790', '39954', '635354', '407948', '3080', '131448', '365760', '533672', '923948', '657072', '565668', '324294', '105692', '762780', '342972', '591090', '752994', '815520', '216732', '99714', '114278', '480072', '632994', '684978', '666770', '997740', '820578', '293640', '411194', '446294', '550490', '933968', '443868', '13298', '214010', '46818', '182060', '383694', '955608', '515612', '717884', '1302', '739494', '572834', '264464', '947652', '565464', '496748', '574160', '651462', '186708', '957212', '464910', '638568', '22470', '238748', '830888', '239964', '94350', '830580', '561104', '403788', '28352', '433370', '943752', '160982', '262338', '641262', '404558', '720060', '171264', '972330', '380198', '351402', '287150', '633474', '338390', '33354', '534404', '169322', '372060', '692730', '52364', '652358', '74204', '419088', '499524', '513158', '648342', '718358', '294564', '335610', '8630', '652658', '408624', '339762', '786420', '73304', '379188', '527348', '148914', '430752', '644198', '200088', '957098', '340284', '278414', '162692', '883878', '335528', '694368', '192818', '96908', '754968', '222008', '653622', '552710', '721208', '300234', '592854', '382650', '336042', '445878', '148868', '217970', '489990', '9030', '443604', '829988', '224712', '187508', '1784', '735422', '600338', '52164', '182142', '582794', '412950', '447678', '869070', '597270', '172002', '625410', '894404', '769620', '53820', '103484', '852990', '332804', '895320', '361512', '331502', '187928', '771218', '66702', '580530', '725808', '530262', '828524', '814644', '877404', '621740', '592484', '442754', '791952', '186552', '9750', '50924', '742608', '794594', '241454', '379178', '177044', '43332', '702828', '426744', '302928', '349914', '656600', '311982', '254700', '376758', '85782', '893220', '337284', '308142', '443040', '615314', '95084', '893568', '127740', '306030', '390782', '90002', '286290', '155664', '723960', '650564', '64680', '482124', '700362', '806858', '217082', '672884', '173714', '489102', '174458', '432054', '241952', '240422', '439800', '611484', '937502', '286928', '371360', '473504', '961134', '10134', '412538', '456350', '945962', '673550', '862418', '426198', '809424', '152810', '931878', '698978', '243840', '473378', '630102', '665222', '351932', '538598', '819812', '53454', '526652', '91944', '473328', '505694', '93494', '511898', '996884', '100610', '787808', '946460', '589328', '317732', '617768', '730188', '554532', '309108', '818430', '170810', '316214', '17342', '855620', '933422', '987798', '939452', '42900', '185492', '970878', '403688', '2544', '157890', '269210', '536672', '9042', '305862', '615188', '143528', '867734', '16128', '182', '210462', '540804', '504390', '880872', '73422', '20232', '606380', '668154', '66404', '507140', '121622', '950334', '157680', '170844', '514562', '110588', '239384', '390114', '733374', '6222', '400754', '746414', '106020', '675974', '325518', '389570', '377790', '800874', '831168', '21408', '612612', '599244', '729614', '486768', '365852', '588738', '897008', '983318', '728994', '71414', '352742', '960738', '220404', '754302', '95604', '581558', '85082', '602040', '473444', '907470', '608498', '26634', '434510', '358230', '327402', '713564', '421350', '528392', '647838', '855672', '717668', '182048', '293484', '179820', '117530', '143528', '754532', '399044', '649262', '895244', '582018', '25764', '655718', '918890', '879798', '511172', '154098', '92204', '897882', '120504', '2790', '1500', '285674', '24152', '196728', '192588', '705644', '779082', '876722', '549644', '532404', '761928', '998540', '311792', '66960', '77478', '191232', '181838', '146678', '549944', '315698', '250920', '576882', '955334', '318824', '620492', '29412', '193608', '16092', '95582', '405818', '765104', '975900', '20850', '474788', '934598', '725520', '683358', '988902', '900624', '728948', '181362', '690120', '155382', '450422', '292460', '60590', '1784', '210128', '952670', '966212', '212124', '313344', '267498', '522000', '510050', '623478', '309092', '133968', '739202', '400314', '866400', '226484', '7220', '692250', '462444', '443190', '846578', '458070', '432924', '304430', '915642', '940760', '402342', '463680', '932022', '508302', '989798', '74288', '360008', '138060', '257540', '80898', '555684', '12792', '678578', '147254', '59264', '16488', '171518', '275340', '5642', '446262', '509522', '279680', '750312', '496314', '193800', '938808', '693878']
I can't copy all the 200,000 integers
Here's one possible solution. It generates a list of primes up to the maximum value of the input numbers, then uses a binary search to find the next largest prime relative to each input number, finally computing the difference between those values and the inputs:
from math import sqrt
def sieve(prime, n, primes):
for i in range(prime*prime, n+1, prime):
primes[i] = False
return primes
def listPrimes(n):
n = n * 2 # to allow for the largest input value not being prime
primes = [False, False] + [True] * (n - 1)
primes = sieve(2, n, primes)
q = int(sqrt(n))
for p in range(3, q+1, 2):
if primes[p]:
primes = sieve(p, n, primes)
return [p for p in range(n+1) if primes[p]]
def findNextPrime(n, primes):
i = len(primes) // 2
if primes[i] >= n:
if i == 0 or primes[i-1] < n:
return primes[i]
return findNextPrime(n, primes[:i])
return findNextPrime(n, primes[i:])
nums = [89, 54, 91, 74, 44, 19, 12]
primes = listPrimes(max(nums))
nextPrimes = [findNextPrime(n, primes) for n in nums]
delta = [np - p for p, np in zip(nums, nextPrimes)]
print(delta)
Output:
[0, 5, 1, 5, 3, 0, 1]
Update
As #PresidentJamesK.Polk points out in the comments, this can be made more efficient by pre-computing the differences between each number and the next prime. This can be done by not filtering the sieve and then traversing it in reverse order to compute the distance from each number to the next prime:
from math import sqrt
def sieve(prime, n, primes):
for i in range(prime*prime, n+1, prime):
primes[i] = False
return primes
def listPrimes(n):
n = n * 2 # to allow for the largest input value not being prime
primes = [False, False] + [True] * (n - 1)
primes = sieve(2, n, primes)
q = int(sqrt(n))
for p in range(3, q+1, 2):
if primes[p]:
primes = sieve(p, n, primes)
return primes
def primeDiffs(primes):
primediffs = []
i = len(primes) - 1
while not primes[i]:
i -= 1
for i in range(i, -1, -1):
if primes[i]:
dist = 0
else:
dist += 1
primediffs.append(dist)
return primediffs[::-1]
nums = [89, 54, 36, 74, 44, 19, 12]
primes = listPrimes(max(nums))
diffs = primeDiffs(primes)
delta = [diffs[n] for n in nums]
print(delta)
Output:
[0, 5, 1, 5, 3, 0, 1]
Note
For both cases, max(nums) should be computed as the numbers are input to save time.
Here's a quick way of doing it, it's faster than 100ms for each item but slower for the full set ( even sieving takes longer than 100ms, and that's a fast algorithm):
from sympy import isprime
import random
import numpy as np
def primes_sieve2(limit):
a = [True] * limit
a[0] = a[1] = False
for (i, isprime) in enumerate(a):
if isprime:
yield i
for n in range(i*i, limit, i):
a[n] = False
PRIMES=np.array(list(primes_sieve2(2750160)), 'uint32')
def create_random_array(limit=len(PRIMES)):
vv = np.zeros(len(PRIMES), 'uint32')
for x in range(limit-1):
vv[x] = random.randint(0,pow(10,6))
return vv
vv = create_random_array()
vvx = np.zeros(len(PRIMES), 'uint32')
count = 0
for xx in vv:
vvx[count] = PRIMES[np.argmax(PRIMES > xx)] - xx
count+=1
vvisprime = np.zeros(len(PRIMES), 'bool')
for x in range(len(PRIMES)):
vvisprime[x] = isprime(int(vvx[x]) + int(vv[x]))
In [49]: vvx
Out[49]: array([ 5, 10, 13, ..., 12, 2, 2], dtype=uint32)
In [48]: vvisprime
Out[48]: array([ True, True, True, ..., True, True, True])
Related
Numerically integrate a function with an array as upper limit
I'm trying to integrate numerically a 1/H(z)dz, where the function H(z) is given by def Hz_th(theta,z): aa,H0,omegam0=theta return np.sqrt(H0**2*(1 + z)**3*omegam0 + 3*H0**2*(1 + z)**3*aa*omegam0 + (1 + z)**3*np.sqrt((4*H0**4*(1 - omegam0))/(1 + z)**6 + (-(H0**2*omegam0) - 3*H0**2*aa*omegam0)**2))/np.sqrt(2) Where z is an array z = np.array([0.50349, 0.4952, 0.6782, 0.8672, 0.7992, 0.37129, 0.35568, 0.28391, 0.46691, 0.94791, 0.69391, 0.89791, 0.78991, 0.59091, 0.2102, 0.8592, 0.5592, 0.9972, 0.5834, 0.7972, 0.9142, 0.94921, 0.7342, 0.7662, 0.9842, 0.7012, 0.83716, 0.37016, 0.35816, 0.52216, 0.70116, 0.74526, 0.59116, 0.62039, 1.2024, 0.6104, 0.35819, 0.45138, 0.55239, 0.75039, 0.82238, 0.95039, 0.34039, 0.8104, 0.61191, 0.54891, 1.02991, 0.42291, 0.70191, 0.68591, 0.93291, 0.74991, 0.46891, 0.95991, 0.8412, 0.5632, 0.8292, 0.57921, 0.8652, 0.8592, 0.58621, 0.4892, 0.68921, 0.32416, 0.18516, 0.68116, 0.92116, 0.42816, 0.69916, 0.57516, 0.51116, 0.64116, 0.35116, 0.34916, 0.35486, 0.87116, 0.92516, 0.4194, 0.89038, 0.48038, 0.58038, 0.80538, 0.66438, 0.74538, 0.7364, 0.37039, 0.85039, 0.21938, 0.64739, 0.60038, 0.76039, 0.47039, 0.49791, 0.50791, 0.58791, 0.69992, 0.76191, 0.78891, 0.40591, 0.80892, 0.58392, 0.72091, 0.83791, 0.80691, 0.50016, 0.53316, 0.82116, 0.12516, 0.44316, 0.68339, 0.72639, 0.40439, 0.69038, 0.57639, 0.72039, 0.55091, 0.8489, 0.43591, 0.30191, 0.58119, 0.34621, 0.3312, 0.44939, 0.2916, 0.46109, 0.63291, 0.26891, 0.92591, 0.62591, 0.60891, 0.57891, 0.7202, 0.7672, 0.3682, 0.8492, 0.77721, 0.58421, 0.5892, 0.7692, 0.5142, 0.63821, 0.43521, 0.58421, 0.66259, 0.62116, 0.41616, 0.45117, 0.73316, 0.74116, 0.34916, 0.51416, 0.64339, 0.4704, 0.26338, 0.7424, 0.91039, 0.3374, 0.98339, 0.81338, 0.8174, 0.96038, 0.75639, 0.87891, 0.80991, 0.62791, 0.93491, 0.69791, 0.76791, 0.86491, 0.51491, 0.47091, 0.92891, 0.6312, 0.6162, 0.5652, 0.55819, 0.7362, 0.2622, 0.7622, 0.8592, 0.5592, 0.48016, 0.92116, 0.47516, 0.89216, 0.63116, 0.73516, 0.41816, 0.53516, 0.73416, 0.60916, 0.70116, 0.98216, 0.75816, 0.64339, 0.51539, 0.80538, 0.80039, 0.71538, 0.76639, 0.90139, 0.71839, 0.57938, 0.96038, 0.64839, 0.93639, 0.46138, 0.67039, 0.24639, 0.63892, 0.53591, 0.77391, 0.37091, 0.69992, 0.37391, 0.85391, 0.60392, 0.53491, 0.40091, 0.37191, 0.84091, 0.53216, 0.93116, 0.55316, 0.84116, 0.72739, 0.44238, 0.2824, 0.5194, 1.06039, 0.26838, 0.2504, 0.69892, 0.73091, 0.99891, 0.40991, 0.12694, 0.25609, 0.08848, 0.11797, 0.18322, 0.1407, 0.15186, 0.09401, 0.28662, 0.26349, 0.1431, 0.16035, 0.12303, 0.12608, 0.17315, 0.16431, 0.1599, 0.24448, 0.24835, 0.27544, 0.33051, 0.33103, 0.16059, 0.21887, 0.11991, 0.17839, 0.27923, 0.18581, 0.06641, 0.258, 0.2798, 0.26576, 0.185, 0.05715, 0.3264, 0.10886, 0.20323, 0.37986, 0.2116, 0.21353, 0.14409, 0.25249, 0.25672, 0.21885, 0.07883, 0.08282, 0.17028, 0.2367, 0.30569, 0.08254, 0.18891, 0.08141, 0.25655, 0.12622, 0.10264, 0.12455, 0.37046, 0.20215, 0.1799, 0.1618, 0.22669, 0.29683, 0.25029, 0.14882, 0.14729, 0.21513, 0.14478, 0.13243, 0.21975, 0.38492, 0.174, 0.17789, 0.29936, 0.11845, 0.17653, 0.24552, 0.19611, 0.24852, 0.30632, 0.24637, 0.13514, 0.21054, 0.2363, 0.17851, 0.22068, 0.31494, 0.18443, 0.26129, 0.18123, 0.38048, 0.18481, 0.20847, 0.17706, 0.31582, 0.24973, 0.20368, 0.12336, 0.20318, 0.15339, 0.33439, 0.1958, 0.24973, 0.15509, 0.15596, 0.15837, 0.30938, 0.24857, 0.18507, 0.18376, 0.18079, 0.17696, 0.17768, 0.0718, 0.13811, 0.1696, 0.24555, 0.14387, 0.25191, 0.03743, 0.27749, 0.28746, 0.19867, 0.15999, 0.21075, 0.23775, 0.15517, 0.09391, 0.15271, 0.25722, 0.21226, 0.2454, 0.13696, 0.32569, 0.10638, 0.40127, 0.18931, 0.19726, 0.12837, 0.12262, 0.12715, 0.21323, 0.21099, 0.26856, 0.20453, 0.2701, 0.24185, 0.29114, 0.35346, 0.28543, 0.21955, 0.17558, 0.23542, 0.15325, 0.21005, 0.29135, 0.31376, 0.13687, 0.29567, 0.30394, 0.20557, 0.1219, 0.17428, 0.24883, 0.24558, 0.13833, 0.24315, 0.24267, 0.31468, 0.27573, 0.30041, 0.21728, 0.20335, 0.19025, 0.29021, 0.22786, 0.31065, 0.13729, 0.08784, 0.08504, 0.19009, 0.09227, 0.12903, 0.26166, 0.26162, 0.19422, 0.14703, 0.21179, 0.17958, 0.19029, 0.3388, 0.11741, 0.2889, 0.26405, 0.24961, 0.22967, 0.08543, 0.27656, 0.36193, 0.30021, 0.11635, 0.15492, 0.25037, 0.25174, 0.12928, 0.30929, 0.29888, 0.27091, 0.29353, 0.18979, 0.12376, 0.31288, 0.30915, 0.20267, 0.21163, 0.1789, 0.30354, 0.21457, 0.32045, 0.21835, 0.07523, 0.2576, 0.18468, 0.11628, 0.06481, 0.10288, 0.04437, 0.16595, 0.16817, 0.17063, 0.12042, 0.13868, 0.2338, 0.24643, 0.22286, 0.17915, 0.19718, 0.22916, 0.26451, 0.10337, 0.21338, 0.15653, 0.2434, 0.23652, 0.20852, 0.25104, 0.22534, 0.28742, 0.32984, 0.24603, 0.11966, 0.32931, 0.21082, 0.20308, 0.28436, 0.18037, 0.2607, 0.14792, 0.32793, 0.24206, 0.22334, 0.1828, 0.15304, 0.13818, 0.20735, 0.20313, 0.19801, 0.22256, 0.2734, 0.18651, 0.15935, 0.21935, 0.17373, 0.24436, 0.13353, 0.28332, 0.17383, 0.19654, 0.36952, 0.20548, 0.17357, 0.12688, 0.28434, 0.19392, 0.10018, 0.37425, 0.11771, 0.19176, 0.23684, 0.15899, 0.07877, 0.13864, 0.23381, 0.15689, 0.08806, 0.13045, 0.18203, 0.25625, 0.21674, 0.14533, 0.13639, 0.20515, 0.36976, 0.1294, 0.26687, 0.27822, 0.17483, 0.11364, 0.11242, 0.15317, 0.19671, 0.18914, 0.17625, 0.21757, 0.18563, 0.16356, 0.25721, 0.12164, 0.16455, 0.19862, 0.26821, 0.40128, 0.14581, 0.05573, 0.22452, 0.28655, 0.18388, 0.10351, 0.33136, 0.21865, 0.26357, 0.16284, 0.10683, 0.16477, 0.1075, 0.12899, 0.05948, 0.04093, 0.01705, 0.01531, 0.01472, 0.02673, 0.01732, 0.04437, 0.01567, 0.02477, 0.038, 0.0299, 0.01012, 0.0258, 0.01038, 0.03791, 0.03507, 0.01268, 0.02525, 0.01908, 0.03489, 0.0295, 0.03416, 0.04067, 0.07515, 0.05581, 0.02529, 0.03501, 0.02846, 0.0328, 0.03378, 0.04597, 0.04341, 0.05684, 0.02024, 0.03191, 0.0415, 0.02639, 0.02651, 0.02689, 0.02349, 0.05067, 0.04093, 0.06877, 0.01754, 0.02557, 0.02189, 0.03155, 0.06845, 0.02249, 0.04949, 0.03161, 0.03434, 0.04486, 0.01531, 0.03048, 0.03837, 0.03682, 0.0221, 0.01402, 0.0589, 0.05895, 0.06888, 0.0204, 0.02894, 0.02384, 0.01451, 0.03292, 0.06455, 0.01663, 0.05359, 0.03163, 0.02751, 0.02559, 0.02401, 0.03713, 0.03408, 0.02454, 0.01697, 0.01629, 0.03184, 0.037, 0.02932, 0.0174, 0.03578, 0.02845, 0.05035, 0.04269, 0.03212, 0.02752, 0.03049, 0.01724, 0.07063, 0.05344, 0.02878, 0.03518, 0.03348, 0.04651, 0.06119, 0.04714, 0.0212, 0.04028, 0.03863, 0.03314, 0.02414, 0.02837, 0.01043, 0.02038, 0.0471, 0.03984, 0.0122, 0.01271, 0.01763, 0.02171, 0.01572, 0.01972, 0.01678, 0.01226, 0.05322, 0.03055, 0.03001, 0.01506, 0.0153, 0.04713, 0.03411, 0.0152, 0.01504, 0.02763, 0.03359, 0.01447, 0.02547, 0.01082, 0.01741, 0.02031, 0.04819, 0.02223, 0.02811, 0.05458, 0.05721, 0.02091, 0.01454, 0.0431, 0.03273, 0.03399, 0.01382, 0.05092, 0.01752, 0.02482, 0.0288, 0.01568, 0.02197, 0.03426, 0.01698, 0.02752, 0.03438, 0.01318, 0.01726, 0.01209, 0.05418, 0.0197, 0.01662, 0.02398, 0.03144, 0.01291, 0.03827, 0.01532, 0.02643, 0.0202, 0.0373, 0.02348, 0.01635, 0.02275, 0.2305, 0.3812, 0.2534, 0.1526, 0.4365, 0.1023, 0.3712, 0.4661, 0.1959, 0.3112, 0.0322, 0.1098, 0.2255, 0.2604, 0.2253, 0.4474, 0.4399, 0.3374, 0.5283, 0.5761, 0.3876, 0.36, 0.347, 0.4391, 0.3492, 0.4305, 0.5088, 0.2888, 0.1727, 0.318, 0.2752, 0.103, 0.2049, 0.2647, 0.2348, 0.2145, 0.4276, 0.4019, 0.3218, 0.4512, 0.1612, 0.3512, 0.2492, 0.0822, 0.1056, 0.3327, 0.1022, 0.2928, 0.1326, 0.2707, 0.1004, 0.3536, 0.4235, 0.3001, 0.1476, 0.2288, 0.1388, 0.1436, 0.0919, 0.1568, 0.2388, 0.1496, 0.5078, 0.2528, 0.2842, 0.2368, 0.3412, 0.2641, 0.349, 0.3212, 0.1352, 0.3065, 0.2335, 0.3006, 0.3676, 0.1692, 0.07976, 0.1806, 0.1806, 0.2307, 0.04582, 0.2212, 0.2006, 0.0755, 0.02627, 0.1912, 0.1777, 0.1852, 0.3491, 0.2712, 0.2921, 0.3704, 0.0963, 0.1808, 0.2748, 0.2454, 0.3618, 0.2318, 0.1466, 0.1592, 0.4198, 0.3192, 0.5196, 0.1506, 0.3026, 0.2106, 0.2396, 0.3316, 0.2006, 0.3606, 0.2426, 0.2212, 0.1206, 0.3412, 0.2504, 0.3804, 0.3106, 0.1994, 0.24, 0.5027, 0.3701, 0.27, 0.2631, 0.268, 0.308, 0.25, 0.2804, 0.3148, 0.3288, 0.3297, 0.2588, 0.2246, 0.2102, 0.3878, 0.3068, 0.3652, 0.2488, 0.2988, 0.1238, 0.3312, 0.3826, 0.2406, 0.1712, 0.2088, 0.2896, 0.2457, 0.1361, 0.1505, 0.1187, 0.2448, 0.3503, 0.6192, 0.2196, 0.0994, 0.4098, 0.5025, 0.3815, 0.2797, 0.2307, 0.4269, 0.5501, 0.3253, 0.3509, 0.3283, 0.4504, 0.06441, 0.06559, 0.3185, 0.1453, 0.1396, 0.4294, 0.2166, 0.3666, 0.3806, 0.4206, 0.1996, 0.5106, 0.4267, 0.3156, 0.5779, 0.4812, 0.3922, 0.1735, 0.3302, 0.2996, 0.2296, 0.2927, 0.2085, 0.2749, 0.3032, 0.359, 0.36, 0.349, 0.6304, 0.3207, 0.51, 0.3669, 0.1384, 0.1792, 0.1717, 0.2708, 0.3473, 0.4796, 0.3192, 0.5192, 0.3802, 0.3712, 0.1808, 0.2236, 0.1412, 0.3055, 0.4045, 0.2856, 0.3012, 0.2637, 0.5006, 0.3051, 0.3857, 0.1985, 0.0906, 0.4774, 0.3312, 0.4712, 0.3306, 0.2607, 0.07141, 0.1812, 0.3379, 0.1221, 0.2501, 0.1604, 0.2988, 0.3892, 0.2928, 0.3288, 0.3056, 0.2858, 0.4292, 0.1792, 0.1896, 0.2488, 0.4096, 0.2416, 0.3376, 0.0706, 0.3456, 0.3306, 0.05495, 0.4231, 0.1072, 0.2854, 0.3254, 0.3347, 0.2907, 0.4404, 0.2411, 0.4607, 0.1194, 0.545, 0.4114, 0.2688, 0.04976, 0.4118, 0.07369, 0.3188, 0.4198, 0.4208, 0.4392, 0.4804, 0.1998, 0.3018, 0.2902, 0.3092, 0.2956, 0.3386, 0.3782, 0.0896, 0.4507, 0.4604, 1.206, 1.33, 1.54, 1.55, 1.7, 1.8, 2.26, 1.914, 1.3, 1.34, 01.02, 0.735, 1.12, 1.23, 1.23, 0.854, 1.37, 0.9752, 0.97, 0.74, 1.39, 1.305, 0.935, 1.014, 1.315, 1.092]) Furtherly I need to use the integral for MCMC sampling of free parameters, contained in theta. I tried the following code to perform the numerical integration over an array def MCMCf(theta,z): aa,H0,omegam0=theta return np.vectorize(lambda x: 1.0/Hz_th(theta,z)) def MCMCfint(theta,z): aa,H0,omegam0=theta return np.vectorize(integrate.quad(MCMCf(theta,z), 0, z)) But it does not work for me. Maybe someone could give an advice? I need to keep the functional form, because I could not provide the values of free parameters, they will be constrained with emcee. There is a full traceback, when I'm trtying to plot the result with random true values as free parameters --------------------------------------------------------------------------- ValueError Traceback (most recent call last) /tmp/ipykernel_17393/1708689059.py in <module> 7 return np.vectorize(integrate.quad(MCMCf(aa,H0,omegam0,z), 0, z)) 8 z=z_values ----> 9 plt.plot(z,MCMCfint(aa_true,H0_true,omegam0_true,z)) /tmp/ipykernel_17393/1708689059.py in MCMCfint(aa, H0, omegam0, z) 5 6 def MCMCfint(aa,H0,omegam0,z): ----> 7 return np.vectorize(integrate.quad(MCMCf(aa,H0,omegam0,z), 0, z)) 8 z=z_values 9 plt.plot(z,MCMCfint(aa_true,H0_true,omegam0_true,z)) ~/.local/lib/python3.8/site-packages/scipy/integrate/quadpack.py in quad(func, a, b, args, full_output, epsabs, epsrel, limit, points, weight, wvar, wopts, maxp1, limlst) 346 347 # check the limits of integration: \int_a^b, expect a < b --> 348 flip, a, b = b < a, min(a, b), max(a, b) 349 350 if weight is None: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
import scipy.special as spec from scipy.integrate import quad import numpy as np array = np.array([0, 1, 2, 3, 4, 5]) # array[i] >= 0 for all i in [0, len(array) - 1].For example, array = np.array([1, 2, 3, 4, 5]) constant = 3 # >= 0. For example, constant = 3 a = array b = constant * a # multiply numpy array by a scalar(it is possible with numpy arrays(: ) c = b * a / constant e3 = spec.expn(3, c) print('Output:', e3) # return a ndarray with the value of the integral e3 en each point specified in c(numpy array) # Output: [1.09691967e-01 2.76136095e-03 1.04786279e-05 5.96855753e-09 4.97790975e-13] # For evaluate the integral e3 from 0 to a[len(a) - 1]: result1 = e3[len(a) - 1] - e3[0] print('Output:', result1) # Output: -0.499999999950223 # Other thing is the integral of e3 from 0 to a[len(a) - 1].You can use quad( return the value and an estimted error) # See https: result2 = quad(lambda x: spec.expn(3, x), 0, c[len(a) - 1]) print('Output:', result2) # Output: (0.3333333333328521, 4.440892098500626e-16)
Invalid Number of Arguments when trying to add arrays inside a list using python
I am trying to understand why I get invalid number of argument in the below code and if there is a way to fix it. here is the code: import numpy as np acc_reading = [] a = np.array([0.11e+00, 1.11e-08, 1.11e-02]) b = np.array([0.12e+00, 1.22e-08, 2.22e-02]) c = np.array([3.11e+00, 3.18e-08, 3.33e-02]) d = np.array([3.41e+00, 4.18e-08, 4.31e-02]) e = np.array([0.55e+00, 1.55e-08, 5.31e-02]) f = np.array([0.66e+00, 1.66e-08, 3.66e-02]) g = np.array([0.66e+00, 1.66e-08, 3.66e-02]) h = np.array([0.66e+00, 1.66e-08, 3.66e-02]) ab = np.add(a,b) cd = np.add(c, d) ef = np.add(e, f) i = np.add(g, h) acc_reading.append(ab) acc_reading.append(cd) acc_reading.append(ef) acc_reading.append(i) kk = np.add(acc_reading[0], acc_reading[1], acc_reading[2], acc_reading[3]) The output of the above code is: ValueError: invalid number of arguments
Read the docs for np.add and np.sum Your lists - 4 terms of size 3 In [213]: acc_reading Out[213]: [array([2.30e-01, 2.33e-08, 3.33e-02]), array([6.52e+00, 7.36e-08, 7.64e-02]), array([1.21e+00, 3.21e-08, 8.97e-02]), array([1.32e+00, 3.32e-08, 7.32e-02])] sum all values to one: In [214]: np.sum(acc_reading) Out[214]: 9.5526001622 sum rows and columns - treating acc_reading as (3,4) array: In [215]: np.sum(acc_reading, axis=0) Out[215]: array([9.280e+00, 1.622e-07, 2.726e-01]) In [216]: np.sum(acc_reading, axis=1) Out[216]: array([0.26330002, 6.59640007, 1.29970003, 1.39320003]) Your attempt to use np.add (which you used correctly earlier) In [217]: np.add(acc_reading[0], acc_reading[1], acc_reading[2], acc_reading[3]) ...: Traceback (most recent call last): File "<ipython-input-217-52b8f942b588>", line 1, in <module> np.add(acc_reading[0], acc_reading[1], acc_reading[2], acc_reading[3]) TypeError: add() takes from 2 to 3 positional arguments but 4 were given giving it just 2 arrays: In [218]: np.add(acc_reading[0], acc_reading[1]) Out[218]: array([6.750e+00, 9.690e-08, 1.097e-01]) a more direct way: In [220]: arr = np.array([a+b, c+d, e+f, g+h]) In [221]: arr Out[221]: array([[2.30e-01, 2.33e-08, 3.33e-02], [6.52e+00, 7.36e-08, 7.64e-02], [1.21e+00, 3.21e-08, 8.97e-02], [1.32e+00, 3.32e-08, 7.32e-02]]) In [222]: arr.sum() Out[222]: 9.5526001622
Pan Tompkins Lowpass filter overflow
The Pan Tompkins algorithm1 for removing noise from an ECG/EKG is cited often. They use a low pass filter, followed by a high pass filter. The output of the high pass filter looks great. But (depending on starting conditions) the output of the low pass filter will continuously increase or decrease. Given enough time, your numbers will eventually get to a size that the programming language cannot handle and rollover. If I run this on an Arduino (which uses a variant of C), it rolls over on the order of 10 seconds. Not ideal. Is there a way to get rid of this bias? I've tried messing with initial conditions, but I'm fresh out of ideas. The advantage of this algorithm is that it's not very computationally intensive and will run comfortably on a modest microprocessor. 1 Pan, Jiapu; Tompkins, Willis J. (March 1985). "A Real-Time QRS Detection Algorithm". IEEE Transactions on Biomedical Engineering. BME-32 (3): 230–236. Python code to illustrate problem. Uses numpy and matplotlib: import numpy as np import matplotlib.pyplot as plt #low-pass filter def lpf(x): y = x.copy() for n in range(len(x)): if(n < 12): continue y[n,1] = 2*y[n-1,1] - y[n-2,1] + x[n,1] - 2*x[n-6,1] + x[n-12,1] return y #high-pass filter def hpf(x): y = x.copy() for n in range(len(x)): if(n < 32): continue y[n,1] = y[n-1,1] - x[n,1]/32 + x[n-16,1] - x[n-17,1] + x[n-32,1]/32 return y ecg = np.loadtxt('ecg_data.csv', delimiter=',',skiprows=1) plt.plot(ecg[:,0], ecg[:,1]) plt.title('Raw Data') plt.grid(True) plt.savefig('raw.png') plt.show() #Application of lpf f1 = lpf(ecg) plt.plot(f1[:,0], f1[:,1]) plt.title('After Pan-Tompkins LPF') plt.xlabel('time') plt.ylabel('mV') plt.grid(True) plt.savefig('lpf.png') plt.show() #Application of hpf f2 = hpf(f1[16:,:]) print(f2[-300:-200,1]) plt.plot(f2[:-100,0], f2[:-100,1]) plt.title('After Pan-Tompkins LPF+HPF') plt.xlabel('time') plt.ylabel('mV') plt.grid(True) plt.savefig('hpf.png') plt.show() raw data in CSV format: timestamp,ecg_measurement 96813044,2.2336266040 96816964,2.1798632144 96820892,2.1505377292 96824812,2.1603128910 96828732,2.1554253101 96832660,2.1163244247 96836580,2.0576734542 96840500,2.0381231307 96844420,2.0527858734 96848340,2.0674486160 96852252,2.0283479690 96856152,1.9648094177 96860056,1.9208210945 96863976,1.9159335136 96867912,1.9208210945 96871828,1.8768328666 96875756,1.7986314296 96879680,1.7448680400 96883584,1.7155425548 96887508,1.7057673931 96891436,1.6520038604 96895348,1.5591397285 96899280,1.4809384346 96903196,1.4467253684 96907112,1.4369501113 96911032,1.3978494453 96914956,1.3440860509 96918860,1.2952101230 96922788,1.3000977039 96926684,1.3343108892 96930604,1.3440860509 96934516,1.3489736318 96938444,1.3294233083 96942364,1.3782991170 96946284,1.4222873687 96950200,1.4516129493 96954120,1.4369501113 96958036,1.4320625305 96961960,1.4565005302 96965872,1.4907135963 96969780,1.5053763389 96973696,1.4613881111 96977628,1.4125122070 96981548,1.4076246261 96985476,1.4467253684 96989408,1.4809384346 96993324,1.4760508537 96997236,1.4711632728 97001160,1.4907135963 97005084,1.5444769859 97008996,1.5982404708 97012908,1.5835777282 97016828,1.5591397285 97020756,1.5786901473 97024676,1.6324535369 97028604,1.6911046504 97032516,1.6959922313 97036444,1.6764417648 97040364,1.6813293457 97044296,1.7155425548 97048216,1.7448680400 97052120,1.7253177165 97056048,1.6911046504 97059968,1.6911046504 97063880,1.7302052974 97067796,1.7741935253 97071724,1.7693059444 97075644,1.7350928783 97079564,1.7595307826 97083480,1.8719452857 97087396,2.0381231307 97091316,2.2482893466 97095244,2.4828934669 97099156,2.7468230724 97103088,2.9960899353 97106996,3.0987291336 97110912,2.9178886413 97114836,2.5171065330 97118756,2.0185728073 97122668,1.5053763389 97126584,1.1094819307 97130492,0.8015640258 97134396,0.5767350673 97138308,0.4545454502 97142212,0.4349951267 97146124,0.4692081928 97150020,0.4887585639 97153924,0.4594330310 97157828,0.4105571746 97161740,0.3861192512 97165660,0.3763440847 97169580,0.3714565038 97173492,0.3225806236 97177404,0.2639296054 97181316,0.2394916772 97185236,0.2297165155 97189148,0.2443792819 97193060,0.2248289346 97196972,0.1857282543 97200900,0.1808406734 97204812,0.2199413537 97208732,0.2492668628 97212652,0.2443792819 97216572,0.2199413537 97220484,0.2248289346 97224404,0.2834799575 97228316,0.3274682044 97232228,0.3665689229 97236132,0.3861192512 97240036,0.4398827075 97243936,0.5083088874 97247836,0.6109481811 97251748,0.7086998939 97255660,0.7771260738 97259568,0.8553275108 97263476,0.9775171279 97267392,1.1094819307 97271308,1.1974585056 97275228,1.2512218952 97279148,1.2952101230 97283056,1.3734115362 97286992,1.4760508537 97290900,1.5493645668 97294820,1.5738025665 97298740,1.5982404708 97302652,1.6471162796 97306584,1.7106549739 97310500,1.7546432018 97314420,1.7546432018 97318340,1.7644183635 97322272,1.8084066390 97326168,1.8621701240 97330072,1.8963831901 97333988,1.8817204475 97337912,1.8572825431 97341840,1.8670577049 97345748,1.8866080284 97349668,1.8768328666 97353580,1.8230694770 97357500,1.7595307826 97361424,1.7302052974 97365332,1.7350928783 97369252,1.6959922313 97373168,1.6226783752 97377092,1.5298142433 97381012,1.4613881111 97384940,1.4320625305 97388860,1.4076246261 97392780,1.3440860509 97396676,1.2658846378 97400604,1.2121212482 97404532,1.1974585056 97408444,1.1779080629 97412356,1.1192570924 97416264,1.0361680984 97420164,0.9628542900 97424068,0.9286412239 97427988,0.9042033195 97431892,0.8406646728 97435804,0.7575757503 97439708,0.6940371513 97443628,0.6793744087 97447540,0.6793744087 97451452,0.6549364089 97455356,0.6060606002 97459240,0.5767350673 97463140,0.6011730194 97467044,0.6451612472 97470964,0.6842619895 97474884,0.6891495704 97478796,0.7184750556 97482700,0.8064516067 97486612,0.8846529960 97490516,0.9335288047 97494428,0.9530791282 97498340,0.9481915473 97502256,0.9726295471 97506156,0.9921798706 97510060,0.9726295471 97513980,0.8846529960 97517884,0.7869012832 97521796,0.7086998939 97525692,0.6549364089 97529604,0.5913978099 97533516,0.4887585639 97537428,0.3567937374 97541348,0.2639296054 97545260,0.2003910064 97549148,0.1417399787 97553060,0.0928641223 97556980,0.0537634420 97560892,0.0342130994 97564804,0.0146627559 97568708,0.0244379281 97572628,0.0048875851 97576500,0.0000000000 97580324,0.0000000000 97584172,0.0097751703 97588060,0.0244379281 97591980,0.0195503416 97595900,0.0146627559 97599812,0.0488758563 97603724,0.1319648027 97607628,0.2248289346 97611548,0.3030303001 97615444,0.3665689229 97619364,0.4496578693 97623276,0.5718474864 97627176,0.7038123130 97631076,0.8064516067 97634988,0.8699902534 97638900,0.9384163856 97642816,1.0361680984 97646720,1.1485825777 97650644,1.2365591526 97654572,1.2658846378 97658492,1.2805473804 97662404,1.3294233083 97666324,1.3782991170 97670244,1.3831867027 97674148,1.3489736318 97678068,1.3049852848 97681988,1.2903225421 97685908,1.3000977039 97689812,1.3098728656 97693728,1.2463343143 97697648,1.1876833438 97701568,1.1681329011 97705488,1.1876833438 97709412,1.1827956390 97713328,1.1339198350 97717244,1.0752688646 97721144,1.0557184219 97725056,1.0703812837 97728972,1.0850440216 97732872,1.0752688646 97736788,1.0459432601 97740696,1.0508308410 97744600,1.0948191833 97748520,1.1290322542 97752444,1.1192570924 97756364,1.0850440216 97760272,1.0801564407 97764168,1.1094819307 97768072,1.1339198350 97771996,1.1143695116 97775920,1.0557184219 97779840,1.0166177749 97783756,0.9970674514 97787668,0.9921798706 97791580,0.9530791282 97795500,0.8846529960 97799412,0.8504399299 97803316,0.8455523490 97807212,0.8699902534 97811124,0.8699902534 97815028,0.8308895111 97818940,0.8064516067 97822844,0.8211143493 97826756,0.8651026725 97830668,0.9042033195 97834572,0.8895405769 97838476,0.8993157386 97842396,0.9530791282 97846304,1.0410556793 97850204,1.0850440216 97854112,1.0899316024 97858020,1.1192570924 97861940,1.2267839908 97865860,1.4320625305 97869772,1.6911046504 97873688,1.9892473220 97877604,2.3020527362 97881524,2.6197457313 97885452,2.8299119949 97889372,2.7761485576 97893292,2.4535679817 97897216,1.9745845794 97901136,1.4956011772 97905052,1.0899316024 97908964,0.8260019302 97912864,0.6695992469 97916772,0.6353860855 97920684,0.7038123130 97924588,0.8553275108 97928496,1.0215053558 97932408,1.1388074159 97936324,1.2023460865 97940228,1.2463343143 97944148,1.3098728656 97948064,1.3734115362 97951988,1.3929618644 97955912,1.3734115362 97959836,1.3636363744 97963764,1.3782991170 97967684,1.4173997879 97971612,1.4173997879 97975540,1.3880742835 97979460,1.3831867027 97983372,1.4027370452 97987292,1.4467253684 97991216,1.4565005302 97995124,1.4320625305 97999036,1.4173997879 98002964,1.4662756919 98006884,1.5249266624 98010812,1.5689149856 98014740,1.5689149856 98018668,1.5689149856 98022592,1.6129032135 98026516,1.6715541839 98030436,1.6911046504 98034348,1.6617790222 98038268,1.6422286987 98042192,1.6715541839 98046124,1.7204301357 98050032,1.7399804592 98053952,1.7155425548 98057880,1.6911046504 98061800,1.7106549739 98065716,1.7595307826 98069636,1.7937438488 98073556,1.7888562679 98077476,1.7741935253 98081408,1.8084066390 98085312,1.8719452857 98089228,1.9305962562 98093144,1.9257086753 98097048,1.9257086753 98100968,1.9599218368 98104884,2.0332355499 98108804,2.0967741012 98112724,2.1016616821 98116652,2.0869989395 98120564,2.0967741012 98124484,2.1456501483 98128404,2.1847507953 98132324,2.1749756336 98136252,2.1212120056 98140156,2.0967741012 98144068,2.1114368438 98147996,2.0967741012 98151908,2.0430107116 98155824,1.9501466751 98159748,1.8817204475 98163664,1.8475073814 98167584,1.8377322196 98171508,1.7937438488 98175440,1.7253177165 98179364,1.7057673931 98183296,1.7106549739 98187200,1.7448680400 98191108,1.7546432018 98195032,1.7302052974 98198952,1.7302052974 98202868,1.7741935253 98206800,1.8426198005 98210712,1.8866080284 98214628,1.8914956092 98218548,1.8914956092 98222472,1.9403715133 98226396,2.0087976455 98230308,2.0527858734 98234212,2.0527858734 98238132,2.0527858734 98242044,2.0869989395 98245964,2.1407625675 98249892,2.1798632144 98253812,2.1749756336 98257740,2.1652004718 98261660,2.1896383762 98265588,2.2385141849 98269516,2.2678396701 98273444,2.2385141849 98277364,2.1896383762 98281292,2.1798632144 98285212,2.1994135379 98289140,2.2091886997 98293052,2.1798632144 98296980,2.1212120056 98300892,2.0918865203 98304804,2.1114368438 98308732,2.1163244247 98312660,2.0674486160 98316572,2.0087976455 98320480,1.9892473220 98324392,1.9892473220 98328308,2.0087976455 98332216,1.9892473220 98336132,1.9501466751 98340048,1.9354838371 98343972,1.9696969985 98347888,1.9843597412 98351812,1.9696969985 98355736,1.9159335136 98359664,1.8866080284 98363576,1.9012707710 98367484,1.9305962562 98371408,1.9208210945 98375324,1.8817204475 98379240,1.8719452857 98383156,1.8817204475 98387072,1.9305962562 98390984,1.9403715133 98394904,1.9159335136 98398832,1.9012707710 98402744,1.9354838371 98406672,1.9794721603 98410584,1.9941349029 98414492,1.9696969985 98418416,1.9550342559 98422336,1.9843597412 98426260,2.0430107116 98430164,2.0723361968 98434076,2.0527858734 98437988,2.0381231307 98441900,2.0625610351 98445820,2.1065492630 98449740,2.1309874057 98453660,2.1065492630 98457572,2.0869989395 98461492,2.0918865203 98465404,2.1456501483 98469324,2.1847507953 98473236,2.1749756336 98477148,2.1505377292 98481052,2.1652004718 98484972,2.1945259571 98488900,2.2287390232 98492820,2.2091886997 98496732,2.1700880527 98500644,2.1652004718 98504556,2.2091886997 98508476,2.2531769275 98512404,2.2336266040 98516324,2.1994135379 98520244,2.2043011188 98524152,2.2531769275 98528068,2.2873899936 98531988,2.2727272510 98535908,2.2238514423 98539836,2.1994135379 98543764,2.2336266040 98547676,2.2580645084 98551588,2.2482893466 98555508,2.1994135379 98559436,2.1652004718 98563356,2.1603128910 98567268,2.1700880527 98571164,2.1309874057 98575068,2.0527858734 98578992,1.9843597412 98582920,1.9648094177 98586840,1.9696969985 98590756,1.9501466751 98594680,1.8963831901 98598596,1.8523949623 98602528,1.8572825431 98606456,1.8621701240 98610376,1.8670577049 98614292,1.8328446388 98618204,1.8132943153 98622132,1.8426198005 98626048,1.8963831901 98629968,1.9257086753 98633892,1.8914956092 98637808,1.8670577049 98641716,1.8914956092 98645640,1.9941349029 98649556,2.1456501483 98653476,2.3313782215 98657404,2.5708699226 98661316,2.8885631561 98665236,3.2306940555 98669148,3.4799609184 98673064,3.4604105949 98676972,3.1769306659 98680884,2.7614858150 98684796,2.2678396701 98688720,1.8230694770 98692628,1.4418377876 98696556,1.2023460865 98700476,1.1241446733 98704400,1.1876833438 98708316,1.3098728656 98712228,1.4125122070 98716148,1.4858260154 98720060,1.5493645668 98723988,1.6275659561 98727908,1.6764417648 98731836,1.6911046504 98735748,1.6617790222 98739676,1.6471162796 98743608,1.6715541839 98747532,1.7057673931 98751452,1.7057673931 98755372,1.6568914413 98759300,1.6275659561 98763220,1.6422286987 98767140,1.6862169265 98771056,1.6911046504 98774964,1.6617790222 98778884,1.6568914413 98782812,1.6911046504 98786728,1.7448680400 98790632,1.7790811061 98794544,1.7693059444 98798452,1.7644183635 98802384,1.7888562679 98806312,1.8279570579 98810224,1.8426198005 98814132,1.8181818962 98818044,1.7986314296 98821972,1.8181818962 98825900,1.8523949623 98829832,1.8719452857 98833760,1.8377322196 98837684,1.8035190582 98841596,1.7986314296 98845528,1.8377322196 98849456,1.8670577049 98853368,1.8523949623 98857292,1.8181818962 98861220,1.8328446388 98865140,1.8866080284 98869048,1.9305962562 98872968,1.9305962562 98876888,1.9012707710 98880800,1.9208210945 98884704,1.9599218368 98888624,1.9892473220 98892544,1.9599218368 98896464,1.8866080284 98900376,1.8426198005 98904296,1.8377322196 98908216,1.8328446388 98912132,1.7839686870 98916040,1.7008798122 98919956,1.6471162796 98923884,1.6373411178 98927812,1.6324535369 98931740,1.5982404708 98935644,1.5151515007 98939564,1.4613881111 98943492,1.4418377876 98947424,1.4271749496 98951348,1.3685239553 98955260,1.2707722187 98959180,1.1925709247 98963092,1.1534701585 98967008,1.1339198350 98970932,1.1045943498 98974840,1.0215053558 98978748,0.9677418708 98982660,0.9579667091 98986572,0.9775171279 98990492,0.9824047088 98994396,0.9237536430 98998308,0.8748778343 99002212,0.8797654151 99006132,0.9188660621 99010036,0.9286412239 99013956,0.9090909004 99017836,0.8895405769 99021740,0.9042033195 99025644,0.9530791282 99029556,0.9921798706 99033468,0.9970674514 99037380,0.9872922897 99041296,1.0166177749 99045196,1.0752688646 99049100,1.1192570924 99053016,1.1290322542 99056940,1.0997067642 99060840,1.1094819307 99064744,1.1485825777 99068668,1.1925709247 99072588,1.2023460865 99076508,1.1925709247 99080428,1.2023460865 99084340,1.2658846378 99088252,1.3343108892 99092168,1.3587487936 99096084,1.3343108892 99100004,1.3294233083 99103924,1.3636363744 99107860,1.4027370452 99111772,1.3831867027 99115700,1.3343108892 99119628,1.3147605657 99123556,1.3343108892 99127480,1.3587487936 99131404,1.3538612127 99135324,1.3049852848 99139236,1.2756597995 99143156,1.2903225421 99147076,1.3196481466 99151012,1.3147605657 99154924,1.2707722187 99158844,1.2072336673 99162764,1.2023460865 99166676,1.2267839908 99170588,1.2365591526 99174508,1.1974585056 99178420,1.1632453203 99182340,1.1534701585 99186248,1.1876833438 99190164,1.1974585056 99194072,1.1583577394 99197996,1.1192570924 99201916,1.1192570924 99205832,1.1730204820 99209748,1.2072336673 99213668,1.2023460865 99217588,1.1779080629 99221488,1.1876833438 99225412,1.2267839908 99229332,1.2707722187 99233244,1.2609970569 99237152,1.2365591526 99241068,1.2463343143 99244988,1.2805473804 99248900,1.2952101230 99252820,1.2805473804 99256732,1.2316715717 99260660,1.2316715717 99264588,1.2854349613 99268512,1.3391984701 99272436,1.3538612127 99276364,1.3343108892 99280292,1.3391984701 99284212,1.3782991170 99288116,1.4271749496 99292040,1.4369501113 99295964,1.4076246261 99299892,1.4076246261 99303816,1.4662756919 99307740,1.5395894050 99311652,1.5640274047 99315564,1.5444769859 99319484,1.5444769859 99323412,1.5786901473 99327332,1.6275659561 99331252,1.6520038604 99335156,1.6422286987 99339076,1.6275659561 99343004,1.6422286987 99346924,1.6666666030 99350844,1.6568914413 99354764,1.6031280517 99358676,1.5542521476 99362604,1.5542521476 99366532,1.5835777282 99370460,1.5982404708 99374372,1.5835777282 99378300,1.5640274047 99382204,1.5835777282 99386132,1.6373411178 99390056,1.6715541839 99393980,1.6520038604 99397892,1.6275659561 99401812,1.6422286987 99405736,1.6862169265 99409664,1.7106549739 99413580,1.6911046504 99417500,1.6568914413 99421432,1.6715541839 99425348,1.7204301357 99429256,1.8084066390 99433164,1.9208210945 99437068,2.0918865203 99440980,2.3655912876 99444912,2.7321603298 99448828,3.0596284866 99452752,3.2453567981 99456680,3.1867058277 99460600,2.9374389648 99464516,2.5610947608 99468428,2.1163244247 99472356,1.6813293457 99476284,1.3343108892 99480200,1.1436949968 99484112,1.1339198350 99488036,1.2365591526 99491956,1.3440860509 99495864,1.4320625305 99499780,1.5298142433 99503708,1.6422286987 99507636,1.7350928783 99511556,1.7644183635 99515480,1.7399804592 99519396,1.7350928783 99523320,1.7448680400 99527220,1.7350928783 99531140,1.6862169265 99535064,1.5933528900 99538980,1.5102639198 99542892,1.4711632728 99546820,1.4467253684 99550748,1.3978494453 99554668,1.3049852848 99558588,1.2072336673 99562504,1.1485825777 99566428,1.1192570924 99570348,1.0752688646 99574256,1.0068426132 99578176,0.9384163856 99582084,0.9188660621 99585988,0.9188660621 99589900,0.9188660621 99593812,0.8895405769 99597716,0.8748778343 99601636,0.8651026725 99605552,0.9090909004 99609436,0.9481915473 99613356,0.9530791282 99617268,0.9237536430 99621180,0.9335288047 99625080,1.0019550323 99628980,1.0752688646 99632888,1.0801564407 99636792,1.0703812837 99640704,1.0899316024 99644616,1.1436949968 99648536,1.2170088291 99652444,1.2170088291 99656356,1.2023460865 99660268,1.2072336673 99664180,1.2561094760 99668084,1.3000977039 99671980,1.3147605657 99675900,1.2952101230 99679820,1.3000977039 99683728,1.3587487936 99687652,1.4027370452 99691568,1.4222873687 99695484,1.3978494453 99699404,1.3880742835 99703328,1.4173997879 99707248,1.4565005302 99711156,1.4760508537 99715064,1.4271749496 99718988,1.3929618644 99722908,1.3929618644 99726828,1.4076246261 99730748,1.3831867027 99734668,1.3147605657 99738580,1.2561094760 99742492,1.2414467334 99746420,1.2658846378 99750340,1.2658846378 99754252,1.2365591526 99758168,1.2121212482 99762084,1.2365591526 99766012,1.3000977039 99769916,1.3538612127 99773856,1.3685239553 99777780,1.3929618644 99781704,1.4662756919 99785620,1.5640274047 99789532,1.6568914413 99793460,1.6959922313 99797392,1.7057673931 99801312,1.7399804592 99805228,1.7937438488 99809148,1.8377322196 99813072,1.8377322196 99816996,1.8230694770 99820920,1.8475073814 99824840,1.9061583518 99828756,1.9501466751 99832680,1.9599218368 99836608,1.9501466751 99840536,1.9599218368 99844452,2.0087976455 99848364,2.0527858734 99852268,2.0527858734 99856184,2.0283479690 99860092,2.0185728073 99864012,2.0576734542 99867932,2.0967741012 99871836,2.0869989395 99875740,2.0478982925 99879652,2.0234603881 99883564,2.0527858734 99887484,2.1065492630 99891404,2.1163244247 99895332,2.0772237777 99899236,2.0527858734 99903156,2.0821113586 99907076,2.1065492630 99910996,2.1016616821 99914916,2.0576734542 99918828,2.0283479690 99922740,2.0430107116 99926652,2.0821113586 99930572,2.1016616821 99934492,2.0576734542 99938404,2.0332355499 99942316,2.0674486160 99946220,2.1309874057 99950124,2.1749756336 99954052,2.1652004718 99957972,2.1260998249 99961892,2.1456501483 99965804,2.1945259571 99969732,2.2336266040 99973644,2.2189638614 99977564,2.1945259571 99981492,2.2043011188 99985404,2.2482893466 99989332,2.2922775745 99993252,2.2580645084 99997164,2.2238514423 100001084,2.2189638614 100005044,2.2678396701 100009004,2.2776148319 100012956,2.2385141849 100016924,2.1798632144 100020892,2.1603128910 100024844,2.1798632144 100028804,2.2140762805 100032756,2.1798632144 100036716,2.1358749866 100040676,2.1163244247 100044644,2.1358749866 100048604,2.1603128910 100052556,2.1407625675 100056516,2.0967741012 100060468,2.0918865203 100064420,2.1163244247 100068384,2.1407625675 100072340,2.1065492630 100076292,2.0478982925 100080244,2.0332355499 100084196,2.0478982925 100088156,2.0674486160 100092100,2.0332355499 100096056,1.9696969985 100100004,1.9110459327 100103968,1.9061583518 100107928,1.9208210945 100111884,1.8768328666 100115844,1.8181818962 100119812,1.7888562679 100123776,1.8084066390 100127712,1.8475073814 100131672,1.8523949623 100135636,1.8181818962 100139604,1.8035190582 100143560,1.8377322196 100147524,1.8768328666 100151488,1.8719452857 100155448,1.8523949623 100159404,1.8132943153 100163376,1.8426198005 100167328,1.8963831901 100171276,1.9110459327 100175232,1.9061583518 100179188,1.9501466751 100183132,2.1016616821 100187084,2.3216030597 100191036,2.5904202461 100194996,2.8787879943 100198956,3.1769306659 100202916,3.4555230140 100206876,3.5826001167 100210836,3.4115347862 100214804,3.0205278396 100218748,2.5317692756 100222708,2.1016616821 100226676,1.7937438488 100230640,1.5933528900 100234592,1.4858260154 100238548,1.5053763389 100242500,1.6422286987 100246456,1.8377322196 100250424,1.9990224838 100254380,2.0967741012 100258340,2.1652004718 100262284,2.2434017658 100266236,2.3411533832 100270196,2.4242424964 100274140,2.4731183052 100278112,2.4975562095 100282068,2.5562071800 100286020,2.6441838741 100289992,2.7028348445 100293948,2.7077224254 100297908,2.7126100063 100301868,2.7468230724 100305820,2.8054740905 100309764,2.8250244140 100313724,2.7908113002 100317676,2.7370479106 100321636,2.7223851680 100325600,2.7419354915 100329556,2.7517106533 100333516,2.7126100063 100337468,2.6735093593 100341428,2.6686217784 100345392,2.7028348445 100349348,2.7272727489 100353316,2.6881721019 100357260,2.6441838741 100361212,2.6588466167 100365180,2.6832845211 100369140,2.7077224254 100373100,2.6783969402 100377052,2.6148581504 100381012,2.6001954078 100384960,2.6246333122 100388916,2.6490714550 100392860,2.6197457313 100396828,2.5659823417 100400788,2.5562071800 100404740,2.5806450843 100408692,2.6099705696 100412644,2.5904202461 100416588,2.5366568565 100420548,2.5268816947 100424508,2.5610947608 100428460,2.5953078269 100432412,2.5757575035 100436372,2.5171065330 100440324,2.4926686286 100444276,2.5219941139 100448228,2.5366568565 100452196,2.5073313713 100456148,2.4389052391 100460108,2.3949170112 100464068,2.3753666877 100468028,2.3655912876 100471988,2.3069403171
The trick seems to be the initial conditions. Load the first 13 values of input and output of low pass filter to zero and the bias goes away. #low-pass filter def lpf(x): y = x.copy() for n in range(13): y[n,1] = 0 x[n,1] = 0 for n in range(len(x)): if(n < 12): continue y[n,1] = 2*y[n-1,1] - y[n-2,1] + x[n,1] - 2*x[n-6,1] + x[n-12,1] return y
Not giving nearest distance from distance matrix in google OR solver
I am using google or tools to get the root from distance matrix but my constrains is to start from nearest distance but solver is giving unexpected route. Below my distance matrix. My expected output is 1 to 10 could in sequence based on distance. data['distance_matrix']=[[1000. 451.13 508.64 543.41 577.64 611.88 646.12 672.58 689.2 1231.78 1246.69] [ 451.13 1000. 484.56 519.33 553.56 587.8 622.04 648.5 665.12 1207.7 1222.61] [ 508.64 460.8 1000. 485.58 519.81 554.05 588.29 614.75 631.37 1173.95 1188.86] [ 543.41 483.77 473.78 1000. 496.84 531.08 565.32 591.78 608.4 1150.98 1165.89] [ 577.64 506.5 496.51 485.34 1000. 508.35 542.59 569.05 585.67 1128.25 1143.16] [ 611.88 529.23 519.24 508.07 496.84 1000. 519.86 546.32 562.94 1105.52 1120.43] [ 646.12 551.96 541.97 530.8 519.57 508.35 1000. 523.59 540.21 1082.79 1097.7 ] [ 672.58 571.36 561.37 550.2 538.97 527.75 516.53 1000. 520.81 1063.39 1078.3 ] [ 689.2 587.98 577.99 566.82 555.59 544.37 533.15 520.81 1000. 1046.77 1061.68] [1231.78 1154.62 1144.63 1133.46 1122.23 1111.01 1099.79 1087.45 1070.83 1000. 495.04] [1246.69 1171.32 1161.33 1150.16 1138.93 1127.71 1116.49 1104.15 1087.53 496.83 10. ]] manager = pywrapcp.RoutingIndexManager(len(data['distance_matrix']), data['num_vehicles'], data['depot']) routing = pywrapcp.RoutingModel(manager) ... transit_callback_index = routing.RegisterTransitCallback(distance_callback) routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index) search_parameters = pywrapcp.DefaultRoutingSearchParameters() search_parameters.first_solution_strategy = ( routing_enums_pb2.FirstSolutionStrategy.PATH_CHEAPEST_ARC) solution= routing.SolveWithParameters(search_parameters) ... Is there any way to add a constraint to solver to get route based on distance value order.if distance value 4, 3, 2 then my route point should be 4, 3, 2. ?
Do you mean for all i if j is nextVar(i) then transit(i-1, i) >= transit(i, j) ? solver = routing.solver() for index in range(routing.size()): if routing.IsEnd(index): continue node = manager.IndexToNode(index) for next_index in range(routing.size()): if routing.IsEnd(next_index) or routing.IsStart(next_index): continue next_node = manager.IndexToNode(next_index) first_cond = routing.NextVar(index) == next_index for next_next_index in range(routing.size()): if routing.IsStart(next_next_index): continue next_next_node = manager.IndexToNode(next_next_index) second_cond = routing.NextVar(next_index) == next_next_index sovler.Add(first_cond * second_cond * distance_matrix[node][next_node] >= first_cond * second_cond * distance_matrix[next_node][next_next_node])
How to find the shortest distance between two line segments capturing the sign values with python
I have a pandas dataframe of the form: benchmark_x benchmark_y ref_point_x ref_point_y 0 525039.140 175445.518 525039.145 175445.539 1 525039.022 175445.542 525039.032 175445.568 2 525038.944 175445.558 525038.954 175445.588 3 525038.855 175445.576 525038.859 175445.576 4 525038.797 175445.587 525038.794 175445.559 5 525038.689 175445.609 525038.679 175445.551 6 525038.551 175445.637 525038.544 175445.577 7 525038.473 175445.653 525038.459 175445.594 8 525038.385 175445.670 525038.374 175445.610 9 525038.306 175445.686 525038.289 175445.626 I am trying to find the shortest distance from the line to the benchmark such that if the line is above the benchmark the distance is positive and if it is below the benchmark the distance is negative. See image below: I used the KDTree from scipy like so: from scipy.spatial import KDTree tree=KDTree(df[["benchmark_x", "benchmark_y"]]) test = df.apply(lambda row: tree.query(row[["ref_point_x", "ref_point_y"]]), axis=1) test=test.apply(pd.Series, index=["distance", "index"]) This seems to work except that it fails to capture the negative values as a result that the line is below the benchmark.
# recreating your example columns = "benchmark_x benchmark_y ref_point_x ref_point_y".split(" ") data = """525039.140 175445.518 525039.145 175445.539 525039.022 175445.542 525039.032 175445.568 525038.944 175445.558 525038.954 175445.588 525038.855 175445.576 525038.859 175445.576 525038.797 175445.587 525038.794 175445.559 525038.689 175445.609 525038.679 175445.551 525038.551 175445.637 525038.544 175445.577 525038.473 175445.653 525038.459 175445.594 525038.385 175445.670 525038.374 175445.610 525038.306 175445.686 525038.289 175445.626""" data = [float(x) for x in data.replace("\n"," ").split(" ") if len(x)>0] arr = np.array(data).reshape(-1,4) df = pd.DataFrame(arr, columns=columns) # adding your two new columns to the df from scipy.spatial import KDTree tree=KDTree(df[["benchmark_x", "benchmark_y"]]) df["distance"], df["index"] = tree.query(df[["ref_point_x", "ref_point_y"]]) Now to compare if one line is above the other or not, we have to evaluate y at the same x position. Therefore we need to interpolate the y points for the x positions of the other line. df = df.sort_values("ref_point_x") # sorting is required for interpolation xy_refpoint = df[["ref_point_x", "ref_point_y"]].values df["ref_point_y_at_benchmark_x"] = np.interp(df["benchmark_x"], xy_refpoint[:,0], xy_refpoint[:,1]) And finally your criterium can be evaluated and applied: df["distance"] = np.where(df["ref_point_y_at_benchmark_x"] < df["benchmark_y"], -df["distance"], df["distance"]) # or change the < to <,>,<=,>= as you wish