Analytic expression to find the range of non-negative numbers in array

Click For Summary
SUMMARY

The discussion centers on deriving a linear or convex analytic expression to calculate the span of indices in an array containing non-negative numbers. The example provided illustrates that for the array p = [0, 0, 0.3, 2.6, 0, 0, 1.1, 0, 1.8, 0], the span of positive elements is calculated as 9 - 3 + 1 = 7. The user, Nazmul, has previously developed a function for binary arrays but seeks a new expression applicable to real numbers. The requirement for linearity or convexity is emphasized due to its application in an optimization problem.

PREREQUISITES
  • Understanding of linear and convex functions
  • Familiarity with array data structures
  • Basic programming skills, particularly in C
  • Knowledge of optimization problems and constraints
NEXT STEPS
  • Research linear and convex function properties in mathematical optimization
  • Explore array manipulation techniques in C programming
  • Investigate analytic expressions for span calculations in numerical analysis
  • Learn about optimization constraints and their implications in algorithm design
USEFUL FOR

Mathematicians, data scientists, software developers, and anyone involved in numerical analysis or optimization problems will benefit from this discussion.

nazmul.islam
Messages
2
Reaction score
0
Hello,

I have an array of length M. Some of the indices are non-negative. I need to derive a function/analytic expression (preferably linear or convex) that finds the span of indices for which the indices are non-negative.

Example: Let p denote the desired vector.

Let, p = [0 0 0.3 2.6 0 0 1.1 0 1.8 0]

There are 10 elements in the array. Non-negative numbers start from index 3 (0.3) and end at index 9 (1.8). Range/span of non-negative numbers = 9-3+1 = 7.

I need a linear/convex function/analytic expression that can find this number.

Previous work:

I have already derived a similar function if the array only contains binary numbers.

Example: let, x = [0 0 1 1 0 0 1 0 1 0].

Now, let M denote the set of indices of x, i.e., M = {1,2,3,...,10}. Assume that x_m denotes the
m-th entry of the array x. For example, x_2 = 0, x_3 = 1. Here, |M| = 10.

My derived function (written in Latex format),
f(x) = max_{m \in M} (m * x_m) - min_{m \in M} (m * x_m + |M| (1 - x_m)) + 1

= max (1 * x_1, 2 * x_2, ..) - min (1 * x_1 + 10 * (1 - x_1), 2 * x_2 + 10 * (1 - x_2), ...) + 1

= 9 - 3 + 1

= 7.

This function is convex and it provides the correct answer. However, it only works for arrays that consist of binary numbers.

I need to find a linear/convex function/analytic expressions which work for arrays that consist of any non-negative number. This function will go into a constraint of my optimization problem. Hence, linearity/convexity is required. However, even a non-linear/non-convex function may help me, too.

Any ideas? Thanks a lot for reading this message. Let me know if I should post it in a different sub-forum of the math forum.

Thanks,

Nazmul
 
Physics news on Phys.org
nazmul.islam said:
Hello,

I have an array of length M. Some of the indices are non-negative.
No. Based on what you have said below, all indexes are positive (1, 2, 3, etc.). Also, no elements of your array are negative, but some of them are zero. It might be a language translation problem. Nonnegative means "not negative" and includes numbers that are either zero or positive.
nazmul.islam said:
I need to derive a function/analytic expression (preferably linear or convex) that finds the span of indices for which the indices are non-negative.
Based on your example, what you're looking for is a function that finds the number of elements that are positive.
nazmul.islam said:
Example: Let p denote the desired vector.

Let, p = [0 0 0.3 2.6 0 0 1.1 0 1.8 0]

There are 10 elements in the array. Non-negative numbers start from index 3 (0.3) and end at index 9 (1.8). Range/span of non-negative numbers = 9-3+1 = 7.
All elements in your array are nonnegative. Some elements are zero and the rest are positive.

Pretty much any programming language can be used to write a simple function that returns the number of positive elements in an array.

In C, the code would look like this:

Code:
int positiveElements(float arr[], int count)
{
  int i;
  int numPos = 0;

  if (count != 0) 
  {
    for (int i = 0; i < count; i++)
    {
      if (arr[i] > 0) numPos++;
    }
  }
  return numPos;
}
nazmul.islam said:
I need a linear/convex function/analytic expression that can find this number.

Previous work:

I have already derived a similar function if the array only contains binary numbers.

Example: let, x = [0 0 1 1 0 0 1 0 1 0].

Now, let M denote the set of indices of x, i.e., M = {1,2,3,...,10}. Assume that x_m denotes the
m-th entry of the array x. For example, x_2 = 0, x_3 = 1. Here, |M| = 10.

My derived function (written in Latex format),
f(x) = max_{m \in M} (m * x_m) - min_{m \in M} (m * x_m + |M| (1 - x_m)) + 1

= max (1 * x_1, 2 * x_2, ..) - min (1 * x_1 + 10 * (1 - x_1), 2 * x_2 + 10 * (1 - x_2), ...) + 1

= 9 - 3 + 1

= 7.

This function is convex and it provides the correct answer. However, it only works for arrays that consist of binary numbers.

I need to find a linear/convex function/analytic expressions which work for arrays that consist of any non-negative number. This function will go into a constraint of my optimization problem. Hence, linearity/convexity is required. However, even a non-linear/non-convex function may help me, too.

Any ideas? Thanks a lot for reading this message. Let me know if I should post it in a different sub-forum of the math forum.

Thanks,

Nazmul
 
Hello Mark44,

Thanks for your reply. I appreciate your effort.

Actually, I probably confused readers by mentioning "non-negative indices". Ideally, I want to find the SPAN of indices that correspond to the "positive elements" in an array. Note that, it is not equal to the number of "positive elements" in an array.

For example, let p = [0 0 0.3 2.6 0 0 1.1 0 1.8 0].

Here, there are 4 positive elements in the array (0.3, 2.6, 1.1, 1.8). However, I want to find the span of indices for which the elements are positive, i.e., the difference between the rightmost and leftmost index for which the element is positive.

In this case, the leftmost index is 3 (corresponding value of the element is 0.3). The rightmost index is 9 (corresponding value of the element is 1.8). Hence, the span is, 9-3+1 = 7.

I know how to find this "span" using a C code. I just need to find an analytic expression that can find this expression when the vector is given.

I have already derived an analytic expression when the elements are binary (shown in my original post). I need to derive an expression where the elements of the vectors are real numbers.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 17 ·
Replies
17
Views
4K
  • · Replies 13 ·
Replies
13
Views
5K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 32 ·
2
Replies
32
Views
3K