By : vvink
Date : November 22 2020, 02:42 PM

To fix this issue The first argument of rollapply must have columns all of the same type, e.g. all numeric so the data frame of the question which has columns of different types cannot be used. Also even if that were fixed and a data.framne with numeric columns only were used, the rollapply function in the question would attempt to pass a matrix to it but the function then uses the matrix as if it were a function. The example in the question is really too large for SO (examples should be cut down to make them minimal yet still illustrate the problem) so here is a smaller example using the built in data frame BOD to illustrate rollapplying a function divide over a data.frame all of whose columns are numeric. code :
library(zoo)
divide < function(demand, Time) sum(demand) / sum(Time)
rollapply(BOD, 3, function(m) divide(m[, "demand"], m[, "Time"]), by.column = FALSE)
## [1] 6.266667 5.033333 4.216667 3.212500
c(divide(BOD[1:3, "demand"], BOD[1:3, "Time"]),
divide(BOD[2:4, "demand"], BOD[2:4, "Time"]),
divide(BOD[3:5, "demand"], BOD[3:5, "Time"]),
divide(BOD[4:6, "demand"], BOD[4:6, "Time"]))
rollapply(BOD, 3, function(m) with(as.data.frame(m), divide(demand, Time)),
by.column = FALSE)
Share :

Compute average distance from point to line segment and line segment to line segment
By : user1929390
Date : March 29 2020, 07:55 AM
this will help If you mean what I think you mean by "average" (and "distance," i.e. the L2 norm mentioned by dreeves), here's a procedure that I think should work for finding the average distance between a point and a line segment. You'll need a function dot(A,B) which takes the dot product of two vectors. code :
// given vectors (points) A, B, C
K1 = dot(AC,AC)
K2 = 2*dot(BA,AC)
K3 = dot(BA,BA)
L1 = sqrt(K3*(K1+K2+K3))
L2 = sqrt(K3*K1)
N = 4*K3*L1 + 2*K2*(L1L2) + (K2*K24*K1*K3)*log((K2+2*L2)/(2*K3+K2+2*L1))
D = N / (8*K3^1.5)

Program using memory segment, waits for lock to have acces to data segment(basic)
By : Anant Sharma
Date : March 29 2020, 07:55 AM
like below fixes the issue I must say i know about semaphors but i dont know how to use them yet. So the thing is I pass the controll to my data segment when the int lock gets a specific value, how could i make my code work cause at this point it freezes and I can't understand why... , The error lies at the beginning of your client's code: code :
int main(int argc, char *argv[]){
if(argc != 2)
perror("~~~ ./c [file name]");
exit(1);
int shmid;
...

Finding the biggest segment in array such that minimum value in it is greater or equal the segment's size
By : san
Date : March 29 2020, 07:55 AM
Does that help This is a nice little puzzle! Here is a solution based on a doubleended queue. My algorithm is mostly the same as yours, except that I use the dequeue to keep track of the minimum values: the queue contains an increasing sequence of values: the first one is the minimum of the current interval, the second one is the minimum of the subinterval that starts after the first minimum, and so on. code :
let segment a =
let len = Array.length a in
let p1 = ref 0 in
let p2 = ref 0 in
let bestlen = ref 0 in
let best = ref [(0,0)] in
let q = Dq.make len in
while !p2 < len do
let l = !p2  !p1 in
if (Dq.is_empty q  Dq.peek_left q > l) && a.(!p2) > l
then begin
while not (Dq.is_empty q) && Dq.peek_right q > a.(!p2) do
Dq.pop_right q;
done;
Dq.push_right q a.(!p2);
incr p2;
end else if !p1 < !p2 then begin
if a.(!p1) = Dq.peek_left q then Dq.pop_left q;
incr p1;
end else begin
assert (Dq.is_empty q);
incr p1;
incr p2;
end;
let l = !p2  !p1 in
if l > !bestlen then begin
bestlen := l;
best := [];
end;
if l = !bestlen then best := (!p1, !p2) :: !best;
done;
!best
;;
module Dq = struct
type t = {
mutable hd : int;
data : int array;
mutable tl : int;
}
let make len = {
hd = 0;
data = Array.make len 0;
tl = 0;
};;
let push_right q x =
assert (q.tl < Array.length q.data);
q.data.(q.tl) < x;
q.tl < q.tl + 1;
;;
let is_empty q = q.tl = q.hd;;
let peek_right q =
assert (q.hd < q.tl);
q.data.(q.tl  1)
;;
let peek_left q =
assert (q.hd < q.tl);
q.data.(q.hd)
;;
let pop_left q =
assert (q.hd < q.tl);
q.hd < q.hd + 1;
;;
let pop_right q =
assert (q.hd < q.tl);
q.tl < q.tl  1;
;;
end

Segment Bar in iOS accross different Size classes
By : andrew
Date : March 29 2020, 07:55 AM
I hope this helps you . You can achieve this by adding UISegmentedControl programatically. Code is as follows in controller viewWillAppear method, code :
 (void)viewWillAppear:(BOOL)animated {
UISegmentedControl * cntrl = [[UISegmentedControl alloc] initWithItems:@[[UIImage imageNamed:@"Circle"],@"2"]];
cntrl.frame = CGRectMake(30, 100, 200, 50);
[self.view addSubview:cntrl];
}
(BOOL) isiPad {
return UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad;
}
[cntrl setImage:[UIImage imageNamed:@"Tick"] forSegmentAtIndex:0];
[cntrl setTitle:@"Hello" forSegmentAtIndex:0];

Regex matching a twosegment word starting with X, and only match the first segment if the second segment ends with Y
By : Paul
Date : March 29 2020, 07:55 AM
Does that help I'm writing a regular expression that will match a twosegment word starting with X. If the second segment doesn't end with Y it'll match both segments, but match the first segment if the second segment ends with Y. , You could try the below,



Related Posts :
