Hi Guillermo,

I've only just created whichND_both, so it's only on git master so far. If 
you're brave enough to install from that, then you can get ahead of everyone 
else and give it a go!

Thank you for explaining the rest of the problem. Another thing I should have 
mentioned, but forgot: there's a magnover (as of 2.090) which takes the 
magnitude of vectors, so you'd be able to say:

      pdl> $rr=$r->magnover
      pdl> ($r1,$r2)=whereND($r->using(0,1),$rr < 0.3)
      pdl> ($r3,$r4)=whereND($r->using(0,1),$rr >= 0.3)

Best regards,
Ed

________________________________
From: Guillermo P. Ortiz <gor...@exa.unne.edu.ar>
Sent: 22 January 2025 22:02
To: Ed . <ej...@hotmail.com>
Cc: pdl-devel@lists.sourceforge.net <pdl-devel@lists.sourceforge.net>; perldl 
<pdl-gene...@lists.sourceforge.net>
Subject: Re: [Pdl-devel] conditional inline

OK Ed,
in my version PDL2.098 still there does not exist whichND_both.
But, let me resume here the problem that I finally solve for a toy example:

pdl> $r=(zeroes(3,3)->ndcoords/2-pdl(0.5,0.5))->r2C
pdl> p $r
[
 [
  [-0.5 -0.5]
  [   0 -0.5]
  [ 0.5 -0.5]
 ]
 [
  [-0.5    0]
  [   0    0]
  [ 0.5    0]
 ]
 [
  [-0.5  0.5]
  [   0  0.5]
  [ 0.5  0.5]
 ]
]
pdl> $rr=($r**2)->sumover->sqrt
pdl> ($r1,$r2)=whereND($r->using(0,1),$rr->abs < 0.3)
pdl> ($r3,$r4)=whereND($r->using(0,1),$rr->abs >= 0.3)

Here we have $r1 and $r2 the x and y coordinates for vectors
that are shorter than 0.3. In $r3 and $r4 the respective coordinates for
vectors that are larger than 0,3

pdl> $r1.=$r1+(1+i)
pdl> $r3.=$r3*(1+i)+$r4
pdl> p $r1
[
 [1+i]
]
pdl> p $r3
[
 [  -1-0.5i      -0.5      0.5i -0.5-0.5i  0.5+0.5i     -0.5i       0.5    
1+0.5i]
]

We are only interested in the x coordinates of $r but of course the modification
depends on y coordinates also. Then, in this example in one case of condition
imposed for the mask we add 1+i and in the complement we multiply for 1+i and
add the y coordinates

pdl> p $r
[
 [
  [-1-0.5i    -0.5]
  [   -0.5    -0.5]
  [   0.5i    -0.5]
 ]
 [
  [-0.5-0.5i         0]
  [      1+i         0]
  [ 0.5+0.5i         0]
 ]
 [
  [ -0.5i    0.5]
  [   0.5    0.5]
  [1+0.5i    0.5]
 ]
]

Obtaining the desired results in $r thanks to dataflow.

Thanks, I hope to be clear now and thanks again for your
patience

Regards





El mié, 22 ene 2025 a las 18:09, Ed . 
(<ej...@hotmail.com<mailto:ej...@hotmail.com>>) escribió:
Hi Guillermo,

I thought you did, but it seemed worth spelling out in case people read the 
mailing-list archive later on :-)

I'm not sure the solution you sent earlier is actually ugly! "If it's stupid 
but it works, it's not stupid." But I am interested to know if whichND_both is 
helpful.

Best regards,
Ed

________________________________
From: Guillermo P. Ortiz <gor...@exa.unne.edu.ar<mailto:gor...@exa.unne.edu.ar>>
Sent: 22 January 2025 21:06
To: Ed . <ej...@hotmail.com<mailto:ej...@hotmail.com>>
Cc: pdl-devel@lists.sourceforge.net<mailto:pdl-devel@lists.sourceforge.net> 
<pdl-devel@lists.sourceforge.net<mailto:pdl-devel@lists.sourceforge.net>>; 
perldl 
<pdl-gene...@lists.sourceforge.net<mailto:pdl-gene...@lists.sourceforge.net>>
Subject: Re: [Pdl-devel] conditional inline

Hi Ed,
yes I realize that problem and use r2C on $r before to apply the mask, this 
work fine.
Also yes for your comment on where_both about. Then, I try with whereND and 
obtain
a ugly solution to my problem I guess

Regards



El mié, 22 ene 2025 a las 17:51, Ed . 
(<ej...@hotmail.com<mailto:ej...@hotmail.com>>) escribió:
There's some quite complicated stuff going on here!

This is a bit flawed conceptually:
      $r1.=$r1*(1+i);
If you say .=, you're telling PDL to take the right-hand side (which will have 
complex type), and assign it into the existing $r1, which is real-typed. The 
only thing it can do is convert the complex-typed data into real data. If you 
change .= to =, you're making the $r1 Perl variable have the complex-typed 
ndarray, which is probably what you'd want here.

This is also:
      ($r1,$r2)=where_both($r,($r**2)->sumover->sqrt < 0.2)
Because where_both does not work n-dimensionally; it takes ($data, $mask), 
calls which_both on the mask (which for a multi-dimensional input will 
broadcast), then indexes (using index1d) a flattened version of $data with that.

Do those points make sense?

Best regards,
Ed

________________________________
From: Guillermo P. Ortiz <gor...@exa.unne.edu.ar<mailto:gor...@exa.unne.edu.ar>>
Sent: 21 January 2025 23:35
To: Ed . <ej...@hotmail.com<mailto:ej...@hotmail.com>>
Cc: pdl-devel@lists.sourceforge.net<mailto:pdl-devel@lists.sourceforge.net> 
<pdl-devel@lists.sourceforge.net<mailto:pdl-devel@lists.sourceforge.net>>; 
perldl 
<pdl-gene...@lists.sourceforge.net<mailto:pdl-gene...@lists.sourceforge.net>>
Subject: Re: [Pdl-devel] conditional inline

Hi Ed,
yes, just I was trying with where_both
But I get unexpected behaviour for me in 2D case

1) first in 1D

pdl> $r=zeroes(3)->ndcoords/2-pdl(0.5)
pdl> p $r
[
 [-0.5]
 [   0]
 [ 0.5]
]

pdl> ($r1,$r2)=where_both($r,$r->abs < 0.2)

pdl> p $r1
[0]

pdl> p $r2
[-0.5 0.5]

now, trying to modify $r following that condition

pdl> $r1.=$r1+1

pdl> $r2.=$r2+3

pdl> p $r

[
 [2.5]
 [  1]
 [3.5]
]

But this seem did not work for complex number

pdl> $r1.=$r1*(1+i)

pdl> $r2.=$r2-(1+i)

pdl> p $r

[
 [1.5]
 [  1]
 [2.5]
]

And, seem did not work in 2D for real case neither

pdl> $r=zeroes(3,3)->ndcoords/2-pdl(0.5,0.5)

pdl> p $r

[
 [
  [-0.5 -0.5]
  [   0 -0.5]
  [ 0.5 -0.5]
 ]
 [
  [-0.5    0]
  [   0    0]
 [ 0.5    0]
 ]
 [
  [-0.5  0.5]
  [   0  0.5]
  [ 0.5  0.5]
]
]

pdl> ($r1,$r2)=where_both($r,($r**2)->sumover->sqrt < 0.2)

pdl> p $r1
[0.5]

pdl> p $r2
[-0.5 -0.5 0 -0.5 -0.5 -0.5 0 0]

what I doing wrong here?

Regards






















El mar., 21 de enero de 2025 19:42, Ed . 
<ej...@hotmail.com<mailto:ej...@hotmail.com>> escribió:
Hi Guillermo,

That does feel to me like you'd want to not do something on the whole ndarray, 
but instead do something on a $pdl->where(condition...). What do you think?

Best regards,
Ed

________________________________
From: Guillermo P. Ortiz <gor...@exa.unne.edu.ar<mailto:gor...@exa.unne.edu.ar>>
Sent: 21 January 2025 19:07
To: Ed . <ej...@hotmail.com<mailto:ej...@hotmail.com>>
Cc: pdl-devel@lists.sourceforge.net<mailto:pdl-devel@lists.sourceforge.net> 
<pdl-devel@lists.sourceforge.net<mailto:pdl-devel@lists.sourceforge.net>>; 
perldl 
<pdl-gene...@lists.sourceforge.net<mailto:pdl-gene...@lists.sourceforge.net>>
Subject: Re: [Pdl-devel] conditional inline

For instance, I am trying with $r, like below

$r=(zeroes(2*$N+1,2*$N+1)->ndcoords-pdl($N,$N))/(2*$N+1)

Regards

El mar, 21 ene 2025 a las 14:39, Guillermo P. Ortiz 
(<gor...@exa.unne.edu.ar<mailto:gor...@exa.unne.edu.ar>>) escribió:
Yes Ed, you are right.
I want to perform different operations on $r
depending on its distance to a point $r0 in 2D.
Then, I guess that first, and because for further
manipulation, I decide to center $r-=$r0 in
such a point.

Then in false code,

if ( length ($r)  < $a)
 { return f($r)}
 else
{ return g($r) }

where "length" I am not sure, but it could be inner($r,$r)->sqrt.
Maybe no ternary expression but some subroutine will be fine also.

Regards












El mar, 21 ene 2025 a las 14:16, Ed . 
(<ej...@hotmail.com<mailto:ej...@hotmail.com>>) escribió:
Hi Guillermo,

That conditional will, for the case you've given, always be false, because it 
has values more than 0.2 away from 0.5. When you use all, you are asking a 
question about the entire ndarray, in other words for every single value in it.

I believe that there's some real problem you're trying to solve, but I'm afraid 
I still have absolutely no idea what it is. Please help me help you!

Best regards,
Ed

________________________________
From: Guillermo P. Ortiz <gor...@exa.unne.edu.ar<mailto:gor...@exa.unne.edu.ar>>
Sent: 21 January 2025 01:19
To: Ed . <ej...@hotmail.com<mailto:ej...@hotmail.com>>
Cc: pdl-devel@lists.sourceforge.net<mailto:pdl-devel@lists.sourceforge.net> 
<pdl-devel@lists.sourceforge.net<mailto:pdl-devel@lists.sourceforge.net>>; 
perldl 
<pdl-gene...@lists.sourceforge.net<mailto:pdl-gene...@lists.sourceforge.net>>
Subject: Re: [Pdl-devel] conditional inline

Ok, Ed,
That example did not work for me.
The conditional results seems to be allways false. I mean, that it give $x also 
when $x is near to 0.5 than 0.2, where I is expecting the $x**2 result.

Regates



El El lun, 20 ene 2025 a la(s) 20:39, Ed . 
<ej...@hotmail.com<mailto:ej...@hotmail.com>> escribió:
Hi Guillermo,

You may still be having a problem, but you have yet to tell us what it is. The 
code you sent works fine, including as many dimensions as you like (because all 
acts on the whole ndarray at once).

If there's a problem in there, please share it :-)

Best regards,
Ed

________________________________
From: Guillermo P. Ortiz <gor...@exa.unne.edu.ar<mailto:gor...@exa.unne.edu.ar>>
Sent: 20 January 2025 23:35
To: Ed . <ej...@hotmail.com<mailto:ej...@hotmail.com>>
Cc: pdl-devel@lists.sourceforge.net<mailto:pdl-devel@lists.sourceforge.net> 
<pdl-devel@lists.sourceforge.net<mailto:pdl-devel@lists.sourceforge.net>>; 
perldl 
<pdl-gene...@lists.sourceforge.net<mailto:pdl-gene...@lists.sourceforge.net>>
Subject: Re: [Pdl-devel] conditional inline

Thanks Ed,
Using ternary conditional expression
I still have some problem with múltiple disensión case.
See my example in message befare


El El lun, 20 ene 2025 a la(s) 19:31, Ed . 
<ej...@hotmail.com<mailto:ej...@hotmail.com>> escribió:
Hi Guillermo,

You can do that indeed, that's just Perl. If you wanted to do operations on a 
subset of that ndarray, then you'd do e.g.

$pdl->where(($pdl-0.5)->abs < 0.2) *= 5;

An observation is that the above condition could be a bit shorter by using the 
recently-added approx_artol: (which would also mean it ran quicker)

$pdl->approx_artol(0.5, 0.2)

Best regards,
Ed

________________________________
From: Guillermo P. Ortiz <gor...@exa.unne.edu.ar<mailto:gor...@exa.unne.edu.ar>>
Sent: 20 January 2025 19:39
To: pdl-devel@lists.sourceforge.net<mailto:pdl-devel@lists.sourceforge.net> 
<pdl-devel@lists.sourceforge.net<mailto:pdl-devel@lists.sourceforge.net>>; 
perldl 
<pdl-gene...@lists.sourceforge.net<mailto:pdl-gene...@lists.sourceforge.net>>
Subject: [Pdl-devel] conditional inline

Hello !

I am not sure, but maybe It is possible to do with perl PDL something
like this?

$ndarray= condition on $ndarray ? assign when true : assign when false

for example:

$x=zeroes(20)->xlinvals(0,1);

$y=(all abs($x-0.5)<0.2)?$x**2:$x;

Thanks in advance

Regards

--


Dr. Guillermo P. Ortiz
Electromagnetismo Aplicado
Dto. Física, Facultad de Ciencias Exactas
Universidad Nacional del Nordeste
Avda Libertad 
5460<https://www.google.com/maps/search/Avda+Libertad+5460?entry=gmail&source=g>,
 Campus UNNE.
W3404AAS Corrientes, Argentina.
(+54) 379-4424678 interno 4613
gortiz at unne edu ar
_______________________________________________
pdl-devel mailing list
pdl-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/pdl-devel

Reply via email to