Is there a vectorized parallel max() and min()?
Sounds like you're looking for pmax
and pmin
("parallel" max/min):
Extremes package:base R Documentation
Maxima and Minima
Description:
Returns the (parallel) maxima and minima of the input values.
Usage:
max(..., na.rm = FALSE)
min(..., na.rm = FALSE)
pmax(..., na.rm = FALSE)
pmin(..., na.rm = FALSE)
pmax.int(..., na.rm = FALSE)
pmin.int(..., na.rm = FALSE)
Arguments:
...: numeric or character arguments (see Note).
na.rm: a logical indicating whether missing values should be
removed.
Details:
‘pmax’ and ‘pmin’ take one or more vectors (or matrices) as
arguments and return a single vector giving the ‘parallel’ maxima
(or minima) of the vectors. The first element of the result is
the maximum (minimum) of the first elements of all the arguments,
the second element of the result is the maximum (minimum) of the
second elements of all the arguments and so on. Shorter inputs
are recycled if necessary. ‘attributes’ (such as ‘names’ or
‘dim’) are transferred from the first argument (if applicable).
Finding maximum from variables in r
You can use Map
. Map applies a function to the corresponding elements of given vectors.
probsA <- c(2.634872e-02, 6.709075e-03, 1.107573e-04, 1.708307e-03, 2.820171e-05)
probsB <- c(0.0013311712, 0.0012295459, 0.0009688963, 0.0011356790, 0.0008949280)
Map(max, probsA, probsB)
[[1]]
[1] 0.02634872
[[2]]
[1] 0.006709075
[[3]]
[1] 0.0009688963
[[4]]
[1] 0.001708307
[[5]]
[1] 0.000894928
Python vectorized running max of parallel line segments
You can use a boolean array to determine if a given point in the space is in a given line segment. That boolean array can be multiplied with the segment values to generate an array where each point on the line has a vector of segment values, and if a segment doesn't include the point, the value is of that segment is zeroed out. From there array's max
method can be applied along a single axis.
import numpy as np
A = np.array([[0,10,4],
[5,19,3],
[6,25,2],
[7,16,1],
[12,21,5]])
# get the dimension of the space
seg_left = A[:, 0, None]
seg_right = A[:, 1, None]
seg_val = A[:, 2, None]
# set the left edge of the space and reset the axes
left_edge = seg_left.min()
seg_left -= left_edge
seg_right -= left_edge
right_edge = seg_right.max()
# generate an array of coordinates and repeat it for each defined segment. This
# can then be used to determine what segments are on for each point
space = np.tile(np.arange(right_edge+1), (seg_val.size, 1))
space_bool = np.logical_and(space >= seg_left,
space < seg_right)
# find the maximum of the on segments
seg_max = (seg_val * space_bool).max(axis=0)
# determine the continuous segments. The +1 ensures that the correct value is
# selected
steps = np.r_[0, np.where(np.diff(seg_max))[0]+1]
seg_val = seg_max[steps[:-1]]
# reset the left edge to the original left edge
steps += left_edge
print(np.c_[steps[:-1], steps[1:], seg_val])
# [[ 0 10 4]
# [10 12 3]
# [12 21 5]
# [21 25 2]]
How can I take pairwise parallel maximum between two vectors?
Pairwise maximum, pmax(a, b)
, will give c(3,4,6)
.
a <- c(3,3,5,NA,1)
b <- c(2,4,6,0,NA)
pmax(a, b)
# [1] 3 4 6 NA NA
pmax(a, b, na.rm = TRUE)
# [1] 3 4 6 0 1
There is also a pairwise minimum
pmin(a, b)
# [1] 2 3 5 NA NA
pmin(a, b, na.rm = TRUE)
# [1] 2 3 5 0 1
And a pairwise sum, which I pulled from this question/answer has been very useful to me at times:
psum(a, b) # == a + b
# [1] 5 7 11 NA NA
psum(a, b, na.rm = TRUE)
# [1] 5 7 11 0 1
psum(c(-1, NA, 4), c(0, NA, NA))
# [1] -1 NA NA
psum(c(-1, NA, 4), c(0, NA, NA), na.rm = TRUE)
# [1] -1 NA 4
psum <- function(..., na.rm = FALSE) {
dat <- do.call(cbind, list(...))
res <- rowSums(dat, na.rm = na.rm)
idx_na <- !rowSums(!is.na(dat))
res[idx_na] <- NA
res
}
R min and max function does not work for dates
We can use pmin/pmax
to get the minimum/maximum when one or both of the input argument is a vector of length greater than 1 (if both have length greater than 1, assumes the length to be same)
df %>%
mutate(earliest=pmin(organe,as.Date("1938-12-1")))
# A tibble: 4 x 3
# locationID organe earliest
# <int> <date> <date>
#1 1 1940-04-01 1938-12-01
#2 2 1938-07-01 1938-07-01
#3 3 1938-07-01 1938-07-01
#4 4 1938-07-01 1938-07-01
Or apply min
after rowwise
df %>%
rowwise %>%
mutate(earliest=min(organe, as.Date("1938-12-1")))
Note that min
returns a single value as output i.e.
min(5:1, 3)
#[1] 1
min(5:3, 1)
#[1] 1
For a vectorized minimum, use pmin
. According to ?min
pmax*() and pmin*() take one or more vectors as arguments, recycle them to common length and return a single vector giving the ‘parallel’ maxima (or minima) of the argument vectors.
Minimum of 2 vectors in R
This is exactly what pmin
is for... which is documented in ?min
.
a <- rnorm(40)
b <- rnorm(40)
minab <- pmin(a,b)
I want to select the greater of the two values from two columns in R
You are looking for the 'pmax' function
Just run this:
pmax(myframe$fwt_r, myframe$fwt_l)
pmax means 'parallel maxima' (or vectorized)
Find max value with ppl.h
There is nothing built in, but it's straightforward with combinable (a reduction variable) and a parallel loop (here parallel_for_each). However if the work you are doing is only 'max' of numbers unless the amount of numbers you are looking at is very large, it may be difficult to see speedups.
You can read more about it on msdn:
#include <ppl.h>
#include <climits>
#include <vector>
#include <numeric>
#include <iostream>
using namespace Concurrency;
int main(int argc, _TCHAR* argv[])
{
std::vector<int> vec(10);
std::iota( begin(vec), end(vec), 1);
combinable<int> locals([]{ return INT_MIN; });
parallel_for_each( begin(vec), end(vec), [&locals](int cur){
auto & localMax = locals.local();
localMax = std::max(cur, localMax);
});
std::cout << "max is " << locals.combine([](int left, int right){ return std::max<int>(left, right);}) << std::endl;
return 0;
}
Replace max and min, second max and second min and so on in numpy
Assuming you want to swap the unique values, you can use np.unique
to get both the sorted unique values and the index of the selected unique value in the sorted unique value. Then, you can revert the array of unique values to swap the min and max values. After that, the index will reference swapped min-max values, so you can just extract them using and indirect indexing. Here is the resulting fully vectorized code:
arr = np.array([1, 2, 1, 3, 2, 4, 1, 4])
uniqueItems, ids = np.unique(arr, return_inverse=True)
out = uniqueItems[::-1][ids]
# out: [4, 3, 4, 2, 3, 1, 4, 1]
Compute the minimum value of every row in a matrix using loops parallel with openmp C++
What we talked about in the comments as an answer: Since I had to write some boilerplate anyway, I used ints as the type and avoided thinking about float problems at all:
#include <vector>
#include <iostream>
using namespace std;
int main(){
constexpr size_t n = 3;
// dummy Distf (int) declared in lieu of matrix Distf
int Distf[n][n] = {{1,2,3},{6,5,4},{7,8,8}};
//could be an array<int,n> instead
vector<int> minRows(n);
#pragma omp parallel for
for (size_t i = 0; i < n; ++i){
int minValue = Distf[i][0];
// Alain Merigot argues this is a performance drag
//#pragma omp parallel for reduction(min : minValue)
for (size_t j = 1; j < n; ++j){
if (Distf[i][j] < minValue){
minValue = Distf[i][j];
}
}
//minRows.push_back(minValue) is a race condition!
minRows[i] = minValue;
}
int k = 0;
for(auto el: minRows){
cout << "row " << k++ << ": " << el << '\n';
}
cout << '\n';
}
The inner loop normally doesn't need to be parallelized. I don't know how many cores you can use, but unless you're on a massively parallel system, think GPU-level of parallelism, the outer loop should either utilize all available cores already, or the problem just isn't big enough to matter. Starting more threads in either situation is a pessimization.
Related Topics
Rscript Does Not Load Methods Package, R Does -- Why, and What Are the Consequences
Apply a Function Over Groups of Columns
Set Default Cran Mirror Permanent in R
What Is the Significance of the New Reference Classes
In R, Use Gsub to Remove All Punctuation Except Period
Create Zip File: Error Running Command " " Had Status 127
Re-Ordering Bars in R's Barplot()
Making a Stacked Area Plot Using Ggplot2
How to Select Last N Observation from Each Group in Dplyr Dataframe
Identify All Objects of Given Class for Further Processing
Setting Document Title in Rmarkdown from Parameters
Rmarkdown: How to Change the Font Color
Linear Regression with a Known Fixed Intercept in R
Generate Markdown Comments Within for Loop