What is the sum() method in php ? Do you mean array_sum ? Or is it the Laravel helper ?
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;
@vincent15000, hey id take an array_sum translation too..
here I'm using a collection method so...
collect($levels)->sum( ...
@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
@vincent15000 I don’t write python. I’m looking for a php equivalent. Not sure my translation to php is correct.
@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
@webrobert Can you give me a short example of the content of levels (in the python context) please ?
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
@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 ?
@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.
@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
@Sinnbeck ok thank you
@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
@vincent15000 so it would be something like that this?
return $levels->filter( fn ($level) => abs($value - $level) < $ave )->isEmpty();
@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.
@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();
@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 );
@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();
@Sinnbeck Yes it works too, it returns true if no level matches the condition.
@webrobert Thank you for the best answer ;).
@Sinnbec, @vincent15000 interesting it was written with sum. I think it maybe makes more sense to me too to use filter.
@webrobert I agree with best answer to @vincent15000. Good work mate
@webrobert Both solutions work ... so you can choose one of them in your code.
@Sinnbeck Oh thank you ;).
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?
@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.
@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
@Sinnbeck, that sounds like a fun project. Take/make and send/post the photos?
@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
@Sinnbeck Oh that's great ... I'd like to be able to do something like that ;).
@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
@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.
@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
@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.
@webrobert maybe I should write a guide someday :)
Please or to participate in this conversation.