Use of statement 'global' in scripts.

2024-05-07 Thread Popov, Dmitry Yu via Python-list
Dear Sirs.

The statement 'global', indicating variables living in the global scope, is 
very suitable to be used in modules. I'm wondering whether in scripts, running 
at the top-level invocation of the interpreter, statement 'global' is used 
exactly the same way as in modules? If there are any differences, I would 
really appreciate any comments on this.

Regards,
Dmitry Popov

Lemont, IL
USA

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Use of statement 'global' in scripts.

2024-05-08 Thread Popov, Dmitry Yu via Python-list
Thank you!

From: Python-list  on behalf of 
Greg Ewing via Python-list 
Sent: Wednesday, May 8, 2024 3:56 AM
To: [email protected] 
Subject: Re: Use of statement 'global' in scripts.

On 8/05/24 1: 32 pm, Popov, Dmitry Yu wrote: > The statement 'global', 
indicating variables living in the global scope, is very suitable to be used in 
modules. I'm wondering whether in scripts, running at the top-level invocation 
of the interpreter,
ZjQcmQRYFpfptBannerStart
This Message Is From an External Sender
This message came from outside your organization.

ZjQcmQRYFpfptBannerEnd

On 8/05/24 1:32 pm, Popov, Dmitry Yu wrote:
> The statement 'global', indicating variables living in the global scope, is 
> very suitable to be used in modules. I'm wondering whether in scripts, 
> running at the top-level invocation of the interpreter, statement 'global' is 
> used exactly the same way as in modules?

The 'global' statement declares a name to be module-level, so there's no
reason to use it at the top level of either a script or a module, since
everything there is module-level anyway.

You only need it if you want to assign to a module-level name from
within a function, e.g.

spam = 17

def f():
   global spam
   spam = 42

f()
# spam is now 42

A script is a module, so everything that applies to modules also
applies to scripts.

--
Greg
--
https://urldefense.us/v3/__https://mail.python.org/mailman/listinfo/python-list__;!!G_uCfscf7eWS!airWCCS1QeLAhk0AfN3VxhuV9MZkx8YBhs5Vjf89K2WZPjhCUkXt9culFzwlX1_ON0G17lukcR79-kWAsA$

-- 
https://mail.python.org/mailman/listinfo/python-list


Version of NymPy

2024-05-15 Thread Popov, Dmitry Yu via Python-list
What would be the easiest way to learn which version of NumPy I have with my 
Anaconda distribution?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Version of NymPy

2024-05-15 Thread Popov, Dmitry Yu via Python-list
Thank you.

From: Larry Martell 
Sent: Wednesday, May 15, 2024 1:55 PM
To: Popov, Dmitry Yu 
Cc: Popov, Dmitry Yu via Python-list 
Subject: Re: Version of NymPy

On Wed, May 15, 2024 at 2: 43 PM Popov, Dmitry Yu via Python-list  wrote: > > What would be the easiest way to learn which version of 
NumPy I have with my Anaconda distribution? >>> import numpy >>>
ZjQcmQRYFpfptBannerStart
This Message Is From an External Sender
This message came from outside your organization.

ZjQcmQRYFpfptBannerEnd

On Wed, May 15, 2024 at 2:43 PM Popov, Dmitry Yu via Python-list
 wrote:
>
> What would be the easiest way to learn which version of NumPy I have with my 
> Anaconda distribution?

>>> import numpy
>>> numpy.__version__
'1.24.4'

-- 
https://mail.python.org/mailman/listinfo/python-list


Relatively prime integers in NumPy

2024-07-11 Thread Popov, Dmitry Yu via Python-list
Dear Sirs.

Does NumPy provide a simple mechanism to identify relatively prime integers, 
i.e. integers which don't have a common factor other than +1 or -1? For 
example, in case of this array:
[[1,5,8],
  [2,4,8],
  [3,3,9]]
I can imagine a function which would return array of common factors along axis 
0: [1,2,3]. Those triples of numbers along axis 1 with the factor of1 or -1 
would be relatively prime integers.

Regards,
Dmitry Popov

Argonne, IL
USA

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Relatively prime integers in NumPy

2024-07-12 Thread Popov, Dmitry Yu via Python-list
Thank you for your interest. My explanation is too concise indeed, sorry. So 
far, I have used Python code with three enclosed 'for' loops for this purpose 
which is pretty time consuming. I'm trying to develop a NumPy based code to 
make this procedure faster. This routine is kind of 'heart' of the algorithm to 
index of X-ray Laue diffraction patterns. In our group we have to process huge 
amount of such patterns. They are collected at a synchrotron radiation 
facility. Faster indexation routine would help a lot.

This is the code I'm currently using. Any prompts how to implement it in NumPy 
would be highly appreciated.

for h in range(0, max_h):
  for k in range(0, max_k):
for l in range(0, max_l):
  chvec=1
  maxmult=2
  if h > 1: 
maxmult=h
  if k > 1:
maxmult=k
  if l > 1:
maxmult=l
  if h > 1:
if maxmult > h:
  maxmult=h
  if k > 1:
if maxmult > k:
  maxmult=k
  if l > 1:
if maxmult > l:
  maxmult=l
  maxmult=maxmult+1
  for innen in range(2, maxmult):
if h in range(0, (max_h+1), innen):
  if k in range(0, (max_k+1), innen):
if l in range(0, (max_l+1), innen):
  chvec=0
  if chvec==1:
# Only relatively prime integers h,k,l pass to this 
block of the code



From: [email protected] 
Sent: Thursday, July 11, 2024 1:22 PM
To: Popov, Dmitry Yu ; 'Popov, Dmitry Yu via Python-list' 

Subject: RE: Relatively prime integers in NumPy

Дмитрий, You may think you explained what you wanted but I do not see what 
result you expect from your examples. Your request is a bit too esoteric to be 
a great candidate for being built into a module like numpy for general purpose 
se but
ZjQcmQRYFpfptBannerStart
This Message Is From an External Sender
This message came from outside your organization.

ZjQcmQRYFpfptBannerEnd

Дмитрий,

You may think you explained what you wanted but I do not see what result you
expect from your examples.

Your request is a bit too esoteric to be a great candidate for being built
into a module like numpy for general purpose se but I can imagine it could
be available in modules build on top of numpy.

Is there a reason you cannot solve this mostly outside numpy?

It looks like you could use numpy to select the numbers you want to compare,
then call one of many methods you can easily search for to see  how to use
python to make some list or other data structure for divisors of each number
involved and then use standard methods to compare the lists and exact common
divisors. If needed, you could then put the results back into your original
data structure using numpy albeit the number of matches can vary.

Maybe a better explanation is needed as I cannot see what your latter words
about -1 and 1 are about. Perhaps someone else knows.




-Original Message-----
From: Python-list  On
Behalf Of Popov, Dmitry Yu via Python-list
Sent: Monday, July 8, 2024 3:10 PM
To: Popov, Dmitry Yu via Python-list 
Subject: Relatively prime integers in NumPy

Dear Sirs.

Does NumPy provide a simple mechanism to identify relatively prime integers,
i.e. integers which don't have a common factor other than +1 or -1? For
example, in case of this array:
[[1,5,8],
  [2,4,8],
  [3,3,9]]
I can imagine a function which would return array of common factors along
axis 0: [1,2,3]. Those triples of numbers along axis 1 with the factor of1
or -1 would be relatively prime integers.

Regards,
Dmitry Popov

Argonne, IL
USA

--
https://urldefense.us/v3/__https://mail.python.org/mailman/listinfo/python-list__;!!G_uCfscf7eWS!ZGK1ZXYgmC6cpNa1xTXVTNklhunjYiinwaDe_xE3sJyVs4ZcVgUB_v2FKvDzDspx7IzFCZI7JpFsiV5iH58P$


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Relatively prime integers in NumPy

2024-07-12 Thread Popov, Dmitry Yu via Python-list
Thank you very much, Oscar.

Using the following code looks like a much better solution than my current 
Python code indeed.

np.gcd.reduce(np.transpose(a))
or
np.gcd.reduce(a,1)

The next question is how I can generate ndarray of h,k,l indices. This can be 
easily done from a Python list by using the following code.

import numpy as np
hkl_list=[]
for h in range(0, max_h):
  for k in range(0, max_k):
for l in range(0, max_l):
  hkl_local=[]
  hkl_local.append(h)
  hkl_local.append(k)
  hkl_local.append(l)
  hkl_list.append(hkl_local)
hkl=np.array(hkl_list, dtype=np.int64)

This code will generate a two-dimensional ndarray of h,k,l indices but is it 
possible to make a faster routine with NumPy?

Regards,
Dmitry





From: Python-list  on behalf of 
Popov, Dmitry Yu via Python-list 
Sent: Thursday, July 11, 2024 2:25 PM
To: [email protected] ; 'Popov, Dmitry Yu via 
Python-list' 
Subject: Re: Relatively prime integers in NumPy

Thank you for your interest. My explanation is too concise indeed, sorry. So 
far, I have used Python code with three enclosed 'for' loops for this purpose 
which is pretty time consuming. I'm trying to develop a NumPy based code to 
make this
ZjQcmQRYFpfptBannerStart
This Message Is From an External Sender
This message came from outside your organization.

ZjQcmQRYFpfptBannerEnd

Thank you for your interest. My explanation is too concise indeed, sorry. So 
far, I have used Python code with three enclosed 'for' loops for this purpose 
which is pretty time consuming. I'm trying to develop a NumPy based code to 
make this procedure faster. This routine is kind of 'heart' of the algorithm to 
index of X-ray Laue diffraction patterns. In our group we have to process huge 
amount of such patterns. They are collected at a synchrotron radiation 
facility. Faster indexation routine would help a lot.

This is the code I'm currently using. Any prompts how to implement it in NumPy 
would be highly appreciated.

for h in range(0, max_h):
  for k in range(0, max_k):
for l in range(0, max_l):
  chvec=1
  maxmult=2
  if h > 1: 
maxmult=h
  if k > 1:
maxmult=k
  if l > 1:
maxmult=l
  if h > 1:
if maxmult > h:
  maxmult=h
  if k > 1:
if maxmult > k:
  maxmult=k
  if l > 1:
if maxmult > l:
  maxmult=l
  maxmult=maxmult+1
  for innen in range(2, maxmult):
if h in range(0, (max_h+1), innen):
  if k in range(0, (max_k+1), innen):
if l in range(0, (max_l+1), innen):
  chvec=0
  if chvec==1:
# Only relatively prime integers h,k,l pass to this 
block of the code



From: [email protected] 
Sent: Thursday, July 11, 2024 1:22 PM
To: Popov, Dmitry Yu ; 'Popov, Dmitry Yu via Python-list' 

Subject: RE: Relatively prime integers in NumPy

Дмитрий, You may think you explained what you wanted but I do not see what 
result you expect from your examples. Your request is a bit too esoteric to be 
a great candidate for being built into a module like numpy for general purpose 
se but
ZjQcmQRYFpfptBannerStart
This Message Is From an External Sender
This message came from outside your organization.

ZjQcmQRYFpfptBannerEnd

Дмитрий,

You may think you explained what you wanted but I do not see what result you
expect from your examples.

Your request is a bit too esoteric to be a great candidate for being built
into a module like numpy for general purpose se but I can imagine it could
be available in modules build on top of numpy.

Is there a reason you cannot solve this mostly outside numpy?

It looks like you could use numpy to select the numbers you want to compare,
then call one of many methods you can easily search for to see  how to use
python to make some list or other data structure for divisors of each number
involved and then use standard methods to compare the lists and exact common
divisors. If needed, you could then put the results back into your original
data structure using numpy albeit the number of matches can vary.

Maybe a better explanation is needed as I cannot see what your latter words
about -1 and 1 are about. Perhaps someone else knows.




-Original Message-
From: Python-list  On
Behalf Of Popov, Dmitry Yu via Python-list
Sent: Monday, July 8, 2024 3:1

Re: Relatively prime integers in NumPy

2024-07-12 Thread Popov, Dmitry Yu via Python-list
Thank you very much. List comprehensions make code much more concise indeed. Do 
list comprehensions also improve the speed of calculations?

From: [email protected] 
Sent: Friday, July 12, 2024 6:57 PM
To: Popov, Dmitry Yu ; 'Popov, Dmitry Yu via Python-list' 
; [email protected] 

Subject: RE: Relatively prime integers in NumPy

Dmitry, I clearly did not understand what you wanted earlier as you had not 
made clear that in your example, you already had progressed to some level where 
you had the data and were now doing a second step. So, I hesitate to say much 
until
ZjQcmQRYFpfptBannerStart
This Message Is From an External Sender
This message came from outside your organization.

ZjQcmQRYFpfptBannerEnd

Dmitry,



I clearly did not understand what you wanted earlier as you had not made clear 
that in your example, you already had progressed to some level where you had 
the data and were now doing a second step. So, I hesitate to say much until 
either nobody else addressed the issue (as clearly some have) or you explain 
well enough.



I am guessing you have programming experience in other languages and are not as 
“pythonic” as some. The code you show may not be quite how others might do it. 
Some may write mch of your code as a single line of python using a list 
comprehension such as:



hkl_list = [ [h, k, l] for SOMETHING in RANGE  for SOMETHING2  in RANGE2 for 
SOMETHING3 in RANGE3]



Where h, k. l come from the somethings.



Back to the real world.





From: Popov, Dmitry Yu 
Sent: Friday, July 12, 2024 1:13 PM
To: [email protected]; 'Popov, Dmitry Yu via Python-list' 
; [email protected]; Popov, Dmitry Yu 

Subject: Re: Relatively prime integers in NumPy



Thank you very much, Oscar.



Using the following code looks like a much better solution than my current 
Python code indeed.

np.gcd.reduce(np.transpose(a))

or

np.gcd.reduce(a,1)



The next question is how I can generate ndarray of h,k,l indices. This can be 
easily done from a Python list by using the following code.



import numpy as np

hkl_list=[]

for h in range(0, max_h):

  for k in range(0, max_k):

for l in range(0, max_l):

  hkl_local=[]

  hkl_local.append(h)

  hkl_local.append(k)

  hkl_local.append(l)

  hkl_list.append(hkl_local)

hkl=np.array(hkl_list, dtype=np.int64)

This code will generate a two-dimensional ndarray of h,k,l indices but is it 
possible to make a faster routine with NumPy?



Regards,

Dmitry









From: Python-list 
mailto:[email protected]>>
 on behalf of Popov, Dmitry Yu via Python-list 
mailto:[email protected]>>
Sent: Thursday, July 11, 2024 2:25 PM
To: [email protected]<mailto:[email protected]> 
mailto:[email protected]>>; 'Popov, Dmitry Yu via 
Python-list' mailto:[email protected]>>
Subject: Re: Relatively prime integers in NumPy



Thank you for your interest. My explanation is too concise indeed, sorry. So 
far, I have used Python code with three enclosed 'for' loops for this purpose 
which is pretty time consuming. I'm trying to develop a NumPy based code to 
make this

ZjQcmQRYFpfptBannerStart

This Message Is From an External Sender

This message came from outside your organization.



ZjQcmQRYFpfptBannerEnd

Thank you for your interest. My explanation is too concise indeed, sorry. So 
far, I have used Python code with three enclosed 'for' loops for this purpose 
which is pretty time consuming. I'm trying to develop a NumPy based code to 
make this procedure faster. This routine is kind of 'heart' of the algorithm to 
index of X-ray Laue diffraction patterns. In our group we have to process huge 
amount of such patterns. They are collected at a synchrotron radiation 
facility. Faster indexation routine would help a lot.



This is the code I'm currently using. Any prompts how to implement it in NumPy 
would be highly appreciated.



for h in range(0, max_h):

  for k in range(0, max_k):

for l in range(0, max_l):

  chvec=1

  maxmult=2

  if h > 1: 

maxmult=h

  if k > 1:

maxmult=k

  if l > 1:

maxmult=l

  if h > 1:

if maxmult > h:

  maxmult=h

  if k > 1:

if maxmult > k:

  maxmult=k

  if l > 1:

if maxmult > l:

  maxmult=l

  maxmult=maxmult+1

  for innen in range(2, max

Assignment of global variables in a script.

2025-06-30 Thread Popov, Dmitry Yu via Python-list
Dear Sirs.

I found the following sentence in the Python documentation: "The statements 
executed by the top-level invocation of the interpreter, either read from a 
script file or interactively, are considered part of a module called 
__main__, 
so they have their own global namespace."

In other words, global assignments of variables placed directly in a module's 
name space are also "statements executed by the top-level invocation of the 
interpreter", if the module is executed as a script.

Is it stable at all to assign global variables directly in a module which runs 
as a script? Speaking practically, I have not observed any problem so far.

Regards,
Dmitry Popov

-- 
https://mail.python.org/mailman3//lists/python-list.python.org