stan-dev / stan-dev/math

Vectorized logical functions

Open
#2,925 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature good first issue new function
Dominant language
C++
Stars
839
Forks
220
Avg merge
2d 4h
Merged PRs (30d)
14

Description

Description

As discussed here, the logical functions don't support vectorization yet.

New functions

Add functions for the logical operators (==,<, <=, >, >=) and their functions (logical_eq, etc.) to allow containers as arguments and support broadcasting. The signatures will be, for example,

int logical_eq(scalar, scalar);
int[] logical_eq(scalars, scalars);
int[] logical_eq(scalar, scalars);
int[] logical_eq(scalars, scalar);

where

  • scalar is int | real | complex
  • scalars is scalar[] | vector | row_vector | complex_vector | complex_row_vector.

If there are two scalar inputs, the existing function is called. If one of the arguments is a container, the other argument must be a container of the same shape or a scalar. With a scalar and container, the scalar is broadcast.

Vectorized logic

We also want to add two functions

int any(scalars);
int all(scalars);

where the first returns 0 if all of the arguments are 0 and 1 otherwise, and the second returns 0 if any of the arguments is 0 and 1 otherwise. We could also add a not function that performs elementwise negation.

Example & Expected Output

data {
  int N;
  real x1[N];
  real x2[N];
}
transformed data {
  real y = sum(x1 > x2);
}

Current error:

No matches for: logical_gt(real[ ], real[ ])
Current Version:

v4.6.2

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the existing scalar logical operators and functions such as logical_eq, logical_gt, and the related logical-functions documentation, then inspect the discussion in issue 202. Done means container and scalar/container combinations support the stated broadcasting and shape rules, with any and all added as specified and the example no longer producing a no-match error.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.