Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

webrobert's avatar

help with python math translation?

in python...

return np.sum([abs(value-level) < ave for _,level in levels]) == 0

and [what I think is] a solution in php...

return $levels->sum( fn ($level) => abs($value - $level) < $ave ) == 0;
0 likes
34 replies
vincent15000's avatar

What is the sum() method in php ? Do you mean array_sum ? Or is it the Laravel helper ?

webrobert's avatar

@vincent15000, hey id take an array_sum translation too..

here I'm using a collection method so...

collect($levels)->sum( ...
vincent15000's avatar

@webrobert Why do you want to user numpy ?

Have you tried something like this ?

return sum(level for level in levels if abs(value - level) < ave) == 0
webrobert's avatar

@vincent15000 I don’t write python. I’m looking for a php equivalent. Not sure my translation to php is correct.

vincent15000's avatar

@webrobert Yep ... I thought you wanted to translate your PHP to Python ... but you try to translate the Pyhton to PHP ... ok ... let me have a look and I come back, I have to live for some 30 minutes

1 like
webrobert's avatar

@vincent15000

here is the code I'm translating...

#method 2: window shifting method
#using the same symbol as the first example above
symbol = 'COST'
df = get_stock_price(symbol)pivots = []
max_list = []
min_list = []
for i in range(5, len(df)-5):
  # taking a window of 9 candles
  high_range = df['High'][i-5:i+4]
  current_max = high_range.max()
  # if we find a new maximum value, empty the max_list 
  if current_max not in max_list:
    max_list = []
  max_list.append(current_max)
  # if the maximum value remains the same after shifting 5 times
  if len(max_list)==5 and is_far_from_level(current_max,pivots,df):
      pivots.append((high_range.idxmax(), current_max))
    
  low_range = df['Low'][i-5:i+5]
  current_min = low_range.min()
  if current_min not in min_list:
    min_list = []
  min_list.append(current_min)
  if len(min_list)==5 and is_far_from_level(current_min,pivots,df):
    pivots.append((low_range.idxmin(), current_min))

plot_all(pivots, df)

I’ve translated everything but that one ☝️ function is_far_from_level

vincent15000's avatar

@webrobert As @sinnbeck says, your PHP code seems to be correct. I don't know this syntax with the conditional sum in Python, I have tried to run your Python sum method, but I get an error about the underscore. Are you sure about it ? I have tried with an array of integers ... what's in your array ?

webrobert's avatar

@vincent15000, not entirely sure...

levels.append((i, high))

so is that?

[
  [1 , 234],
  [4 , 234],
]

I'll go finish wiring it up and see how it works.

Sinnbeck's avatar

@vincent15000 normally you use underscore as a placeholder for variables you don't need

Say you get key and value, but you only need value. Then you would name the key _ to show other programmers (or yourself) that it isn't used. If the key comes first it the function called you are forced to get it even though you don't need it

1 like
vincent15000's avatar

@webrobert Ok I have it ... your python code calculates the number of levels that match the condition. It does not calculate the sum of the level values that match the condition.

For example with theses values, np.sum([abs(value-level) < ave for _,level in levels]) returns 2.

levels = [[1 , 234], [4 , 234], [5, 100]]
value = 500
ave = 400
Sinnbeck's avatar

@vincent15000 so it would be something like that this?

return $levels->filter( fn ($level) => abs($value - $level) < $ave )->isEmpty();
1 like
vincent15000's avatar

@Sinnbeck I just updated my previous answer with an example.

I tried to run your PHP code in Laravel to test the result, but I get an unsupported operand types error.

MESSAGE UPDATED => I had an error in my array definition, it's ok now.

Sinnbeck's avatar

@vincent15000 oh I assumed the levels array was something like levels = [1 ,4 ,5]

I'm just wrapping my head around how it's parsing both numbers.

If it only uses the second number, it would be

return $levels->filter( fn ($level) => abs($value[1] - $level) < $ave )->isEmpty(); 
1 like
vincent15000's avatar
Level 63

@webrobert I just tested your PHP code with success ... your good PHP code is :

collect($levels)->sum( fn ($level) => abs($value - $level) < $ave )

It returns exactly the same result as the Python code.

I have tested with the same values and the $number is 2.

$levels = [1 => 234, 4 => 234, 5 => 100];
$value = 500;
$ave = 400;
$number = collect($levels)->sum( fn ($level) => abs($value - $level) < $ave );
1 like
Sinnbeck's avatar

@vincent15000 then I assume this will work as well :) it won't give you a number, but will be empty if all results are above $ave

return $levels->filter( fn ($level) => abs($value - $level) < $ave )->isEmpty();
1 like
Sinnbeck's avatar

I'm no python wizard but I think it's correct. Don't you have some data you can plot in to check the result?

webrobert's avatar

@Sinnbeck, I have found useful examples with an excel counterpart (for some trading formulas). In this case, I'd be manually comparing values. Seemed it might be faster to get a quick check here, and also see if our community had many coding polyglots.

Sinnbeck's avatar

@webrobert I've coded a bit of python (a photo booth for my wedding) but I'm no expert. But the code is pretty simple and I agree with your conversion. A very simple test is to make 10 random arrays of values and run it through the python version and get the outcome and do the same for php (same values). If they aren't the same, then at least you can give us example of expected output for certain arrays

Maybe if I am by a computer later I will try myself

1 like
Sinnbeck's avatar

@webrobert oh it was. I hooked up a rasperry pi to a Canon camera, a computer screen and a arcade button (one of those big ones red ones). When you hit the button, a countdown would begin and take 3 photos at 0

1 like
Sinnbeck's avatar

@vincent15000 trust me. You can. Just learn to break down any task to its tiniest possible component. If you can debug a python script like this, you can build a photo booth

It took me 3 months and I had never written a line of python when I began

1 like
vincent15000's avatar

@Sinnbeck I already wrote some code in C for small pedagogic robots. So coding a rasberry will not be difficult. But what seems difficult to me is to connect the raspberry to a camera and to pilot the camera from the code.

Sinnbeck's avatar

@vincent15000 I simply found custom firmware for the camera that let me trigger the "take picture" command on the camera, via the USB camera. And then found a cli tool that could send that command from python. It even let me get a live preview and other things

Sorry @webrobert I won't hijack your thread anymore

1 like
webrobert's avatar

@Sinnbeck hijack away, and i did ask. A handful of years ago. I was in my thirties. I had fallen in love with a hot little twenty-something blonde. She wanted to couchsurf around the world. So we did. It was an amazing experience to say the least. Living if only for a moment In different cultures. In people's homes. Anyway, I always wanted to build a photobooth in my home where visiting couchsurfers could use the big red button to make a snap of their stay at my house.

Please or to participate in this conversation.