Re: Best (simplest) way to share data

2024-07-11 Thread Chris Green via Python-list
Stefan Ram  wrote:
> Chris Green  wrote or quoted:
> >That's exactly the sort of solution I was wondering about.  Is there a
> >ready made module/library for handling this sort of thing?  Basically
> >it will just be a string of a few tens of characters that would be
> >kept up to date by one process and asked for by all the others.
> 
>   I'm not an expert here, and just quickly tried to make it
>   run, so the code will still contain errors and not contain
>   something necessary, but might give you a starting point.
> 
>   A process doing something (here: printing an incrementing value
>   named "info") and also serving requests from other processes
>   for this "info" value:
> 
[snip]

Thanks, that should get me started!  :-)

-- 
Chris Green
·
-- 
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


python repl vi mode line editing not working.

2024-07-11 Thread Tobiah via Python-list

Kubuntu 24.04.


sinewave:toby ~(1)> cat .inputrc
set editing-mode vi
set keymap vi
sinewave:toby ~(1)> cat .editrc
bind -v
bind \\t rl_complete
sinewave:toby ~(1)> python
Python 2.7.18 (default, Jul  8 2024, 12:49:12)
[GCC 13.2.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
1  

1

2

2

^[k



I see the literal 'escape' character + 'k', when it should
let me edit previous commands.

I did have to compile my own python because I'm using 2.7 on
this machine.

Thanks for any help.


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


Problem using mysql library

2024-07-11 Thread Tobiah via Python-list

sinewave:toby ~(1)> python
Python 2.7.18 (default, Jul  8 2024, 12:49:12)
[GCC 13.2.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.

import MySQLdb

Traceback (most recent call last):
  File "", line 1, in 
  File "/usr/local/lib/python2.7/site-packages/MySQLdb/__init__.py", line 23, in 

(version_info, _mysql.version_info))
ImportError: this is MySQLdb version (1, 2, 5, 'final', 1), but _mysql is 
version (1, 4, 6, 'final', 0)


I Googled this a lot, and saw many people with the same problem,
but couldn't find an answer that helped.


Thanks!


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


Re: python repl vi mode line editing not working.

2024-07-11 Thread Tobiah via Python-list

   For this to work, the Python implementation should use the same
   readline library as your shell, I guess.


It works in python3, so I guess my problem is that I'm
compiling python (I think kubuntu dropped python2), but
I don't see any relevant options  in the configure help.





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


Re: python repl vi mode line editing not working.

2024-07-11 Thread Tobiah via Python-list

I see the literal 'escape' character + 'k', when it should
let me edit previous commands.

I did have to compile my own python because I'm using 2.7 on
this machine.



I figured it out.  I needed to apt install libreadline-dev.
--
https://mail.python.org/mailman/listinfo/python-list


RE: Relatively prime integers in NumPy

2024-07-11 Thread AVI GROSS via Python-list
Дмитрий,

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://mail.python.org/mailman/listinfo/python-list

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


Re: Relatively prime integers in NumPy

2024-07-11 Thread Oscar Benjamin via Python-list
(posting on-list this time)

On Thu, 11 Jul 2024 at 15:18, Popov, Dmitry Yu via Python-list
 wrote:
>
> 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.

It sounds like you want the gcd (greatest common divisor) of each row.
The math module can do this:

In [1]: a = [[1,5,8],
   ...:   [2,4,8],
   ...:   [3,3,9]]

In [2]: import math

In [3]: [math.gcd(*row) for row in a]
Out[3]: [1, 2, 3]

NumPy can also do it apparently:

In [10]: np.gcd.reduce(np.transpose(a))
Out[10]: array([1, 2, 3])

https://en.wikipedia.org/wiki/Greatest_common_divisor

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


RE: Relatively prime integers in NumPy

2024-07-11 Thread AVI GROSS via Python-list
OK. That explains a bit more.

 

If I understand what you are looking for is a fast implementation and quite 
often in Pyhon it means using code written in another language such as C that 
is integrated carefully in a moule or two. Another tack is to replace many 
explicit loops with often much faster vectorized operations. Numpy provides 
advantages like the above if you use it as intended.

 

Of course there are other techniques in how code is refactored or the order of 
operations, or doing things in parallel.

 

Just as an example, your inner loop ear the top is operating one at a time or 
numbers between 0 and max_l and hen creates variables initialized and then 
possibly changed in chvec and maxmult. It uses various conditions to change 
those variables then goes on to do more things included in a fourth nested loop.

 

What would happen if, instead, you used two objects with the same names that 
were each a numpy array, or perhaps combined into a dataframe type object?

 

Using numpy (and perhaps pandas) you could have code that initialized one such 
array to hold the initial 1 or 2 as needed in an object whose length was 
max_l+1 and then the next operations, using numpy notation would be along the 
lines of replace the corresponding value depending on external variables you 
call h or k and so on. 

 

There would be several invisible loops, perhaps chained in some way, but 
probably running way faster than the explicit loop.

 

I am not going to write any specific code, but suggest you read some 
documentation on how to use numpy for some of the operations you want when 
operating on larger clusters of info. You can gain some speed even by changing 
a few parts. To refactor the entire thing would take more thought and if you 
come up with the idea  of operating on a multidimensional array, might take 
some care. 

 

But consider what would happen if you looked at your loops which are currently 
of a fixed size and created  a 3-D matrix with dimensions of max_h+1, max_k+1, 
and max_l+1 and simply initialized it with all possible initial values and then 
ran an algorithm to manipulate it, often asking numpy for various slices or 
whatever works for you as in axes.  This architecture may not work for ou but 
is an example of the kind of thinking it an take to make a problem use 
algorithms more efficiently.

 

I note the code did not actually help me understand what mathematical operation 
you want to perform. I assumed I might see some operations like division and t 
may be other parts of your code that implement what you want.

 

But if this is a common enough need, I suspect you may want to see if something 
similar enough is out there. Your code may be more complex and more like the 
sieve of Eratosthenes that attempts to test every possibility.

 

One algorithm I have seen simply takes the numbers you are evaluating and in a 
loop of the first N primes (or an open-ended generator) simply does an integer 
division by 2, as many times as it returns an integral result, then as many 
divisions by 3 then 5 and 7 and so on.  It aborts when it has been chopped down 
to size, or the prime being used is large enough (square root or so) ad at the 
end, you should have some sequence of divisors, or just  and the number if it 
is prime. Some such algorithm can be fairly fast and perhaps can even be done 
vectorized. 

 

One last comment is about memoization. If your data is of a nature where a 
relatively few numbers come up often, then you an use something, like perhaps a 
dictionary, to store the results of a computation like getting a list of prime 
factors for a specific number, or just recording whether it is prime or 
composite. Later calls to do calculations would always check if the result has 
already been saved and skip recalculating it.

 

Good Luck

 

 

From: Popov, Dmitry Yu  
Sent: Thursday, July 11, 2024 3:26 PM
To: avi.e.gr...@gmail.com; '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 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