Union of intervals

Sort them by one of the terms (start, for example), then check for overlaps with its (right-hand) neighbor as you move through the list. class tp: def __repr__(self): return “(%d,%d)” % (self.start, self.end) def __init__(self, start, end): self.start = start self.end = end s = [tp(5, 10), tp(7, 8), tp(0, 5)] s.sort(key=lambda self: self.start) y … Read more

C: How to wrap a float to the interval [-pi, pi)

Edit Apr 19, 2013: Modulo function updated to handle boundary cases as noted by aka.nice and arr_sea: static const double _PI= 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348; static const double _TWO_PI= 6.2831853071795864769252867665590057683943387987502116419498891846156328125724179972560696; // Floating-point modulo // The result (the remainder) has same sign as the divisor. // Similar to matlab’s mod(); Not similar to fmod() – Mod(-3,4)= 1 fmod(-3,4)= -3 … Read more

Python – Rounding by quarter-intervals

This is a general purpose solution which allows rounding to arbitrary resolutions. For your specific case, you just need to provide 0.25 as the resolution but other values are possible, as shown in the test cases. def roundPartial (value, resolution): return round (value / resolution) * resolution print “Rounding to quarters” print roundPartial (10.38, 0.25) … Read more

Find which interval row in a data frame that each element of a vector belongs in

Here’s a possible solution using the new “non-equi” joins in data.table (v>=1.9.8). While I doubt you’ll like the syntax, it should be very efficient soluion. Also, regarding findInterval, this function assumes continuity in your intervals, while this isn’t the case here, so I doubt there is a straightforward solution using it. library(data.table) #v1.10.0 setDT(intervals)[data.table(elements), on … Read more

format interval with to_char

you could cast the result if you want less precision: SQL> SELECT TO_DSINTERVAL(’10 10:00:00′) t_interval FROM dual; T_INTERVAL ———————————————————– +000000010 10:00:00.000000000 SQL> SELECT CAST(TO_DSINTERVAL(’10 10:00:00′) 2 AS INTERVAL DAY(2) TO SECOND(3)) t_interval 3 FROM dual; T_INTERVAL ———————————————————– +10 10:00:00.000 Edit following OP comment: From The Oracle Documentation (11gr1): Interval datatypes do not have format models. … Read more

How can I plot data with confidence intervals?

Here is a plotrix solution: set.seed(0815) x <- 1:10 F <- runif(10,1,2) L <- runif(10,0,1) U <- runif(10,2,3) require(plotrix) plotCI(x, F, ui=U, li=L) And here is a ggplot solution: set.seed(0815) df <- data.frame(x =1:10, F =runif(10,1,2), L =runif(10,0,1), U =runif(10,2,3)) require(ggplot2) ggplot(df, aes(x = x, y = F)) + geom_point(size = 4) + geom_errorbar(aes(ymax = … Read more