Show that S is an arithmetic progression, then A can be recognize as a language - dfa

A set S of nonnegative integers is called an arithmetic progresion if there exist some integers n and p such that
S = {n + ip : i ≥ 0}
Let A ⊆ {a}∗ and consider S = {| x |: x ∈ A}.
(1) Show that if S is an arithmetic progression, then A can be recognized by a
DFA
(2) Show that if A can be recognized by a DFA, then S is the union of a finite number of arithmetic

Related

Access elements of group given by finite presentation in Magma Calculator

I have been trying to use the Magma Calculator: http://magma.maths.usyd.edu.au/calc/
Given a word u, in a finitely presented group, how do I declare g to be the group element represented by u?
Context:
I can define a group via a finite presentation. For example using the code:
G<a,b,c> := Group< a,b,c | a^2=b^2, b^2=c^2, c=a*b >;
If I then ask for the order of the group:
Order (G);
The correct answer of 8 is returned, so it does understand what G is.
However I want to know how to ask if two elements of the group are equal.
The problem is that a,b,c as well as G.1, G.2, G.3 denote elements of the free group generated by a,b,c. Similarly products of those symbols (and their inverses) represent words in the free group.
Thus
a*a^-1 eq a^-1*a;
returns true, as it is true in the free group, but
a^2 eq c^2;
returns false, even though it is true in the group G.
This is explained in https://magma.maths.usyd.edu.au/magma/handbook/text/851.
In the section "Operations on words" it says that:
"u eq v : Returns true if the words u and v are identical when freely reduced. NB When G is not a free group and false is returned, this does not imply that u and v do not represent the same element of G."
However Magma can do operations with group elements, including answering if two elements g,h, are the same:
g eq h;
The question is then, given a word u, in a finitely presented group, how do I declare g to be the group element represented by u?
Following the anwer by #Userulli I typed
G<a,b,c> := Group< a,b,c | a^2=b^2, b^2=c^2, c=a*b >;
u := a^2;
v := c^2;
g := ElementToSequence(G, u);
h := ElementToSequence(G, v);
g eq h;
and got the reply
>> g := ElementToSequence(G, u);
^
Runtime error in 'ElementToSequence': Bad argument types
Argument types given: GrpFP, GrpFPElt
>> h := ElementToSequence(G, v);
^
Runtime error in 'ElementToSequence': Bad argument types
Argument types given: GrpFP, GrpFPElt
>> g eq h;
^
User error: Identifier 'g' has not been declared or assigned
Have you tryied using the ElementToSequence method?
For example :
u := a^2;
g := ElementToSequence(u, G);
Then you can compare g with other elements in the group to see if they are the same:
v := c^2;
h := ElementToSequence(v, G);
h eq g; // this should return true
EDIT:
Stating the doc magma.maths.usyd.edu.au/magma/handbook/text/208 you need to use fields instead, so you should convert the group into a field, and then use ElementToSequence method to compare the two sequences?

Rounding to the nearest multiple of a given value

If we have an arbitrary double value f, another one v and a multiplication factor p, how can I snap the value f to the nearest v power of p?
Example:
f = 3150.0
v = 100.0
p = 2
the multiplications will go like this
100 (v)
200 (multiplied by p)
400
800
1600
3200
...
f is closest to 3200.0 so the function should return 3200.0
There was actually a name for this, which I seem to have forgotten and maybe this is why I couldn't find such a function.
Let k = floor(log_p(f/v)) where log_p(x) = log(x)/log(p) is the logarithm to base p function. It follows from the properties of floor and log that p^k v <= f < p^(k+1) v, which gives the two closest values to f of the form p^n v.
Which of those two values to choose depends on the exact definition of "nearest" in your use-case. If taken in the multiplicative sense (as would be natural on a log scale), that "nearest" value can be calculated directly as p^n v where n = round(log_p(f/v)) = round(log(f/v)/log(p)).

numpy: distinguish/convert between different types of nan?

I've been seeing a lot of errors like:
FloatingPointError: invalid value encountered in multiply
on some data I'm loading from disk (using astropy.io.fits). It appears to be related to this issue, i.e., I have a 'signalling nan' instead of a 'quiet nan'.
This is problematic, because I can't simply 'clean' the data. If I try to convert the array to an array with the same dtype, e.g.:
arr = arr.astype(arr.dtype)
the nan stays the same, i.e. np.isnan generates a warning, though if I change the dtype
# arr.dtype is float32 originally
arr = arr.astype(np.float64)
the warning goes away for multiplication/np.isnan/etc. I don't want to use this workaround since it necessitates changing the size of the array.
So, how can I distinguish between those without reverting to the string representation of the nan? Is there a (cheap!) way to convert all 'signalling' nans to quiet ones?
This will replace all the nans in arr with the default quiet nan:
with np.errstate(invalid='ignore'):
arr[np.isnan(arr)] = np.nan
For what it's worth, here's a quick issnan function that is True for signaling nans only:
import numpy as np
def issnan(a):
"""
Returns True where elements of `a` are signaling nans.
`a` must be a numpy array with data type float32 or float64.
This function assumes IEEE 754 floating point representation, and
that the first (left-most) bit in the mantissa is the "quiet" bit.
That is, a nan value with this bit set to 0 is a signaling nan.
"""
if a.dtype == np.float64:
v = a.view(np.uint64)
# t1 is true where all the exponent bits are 1 and the
# quiet bit is 0.
t1 = (v & 0x7FF8000000000000) == 0x7FF0000000000000
# t2 is non-zero where at least one bit (not including
# the quiet bit) in the mantissa is 1. (If the mantissa
# is all zeros and the exponent is all ones, the value is
# infinity.)
t2 = v & 0x0007FFFFFFFFFFFF
return np.logical_and(t1, t2)
elif a.dtype == np.float32:
v = a.view(np.uint32)
t1 = (v & 0x7FC00000) == 0x7F800000
t2 = v & 0x003FFFFF
return np.logical_and(t1, t2)
else:
raise ValueError('a must have dtype float32 or float64')
For example,
In [151]: z
Out[151]: array([ nan, nan, inf, 1.], dtype=float32)
In [152]: [hex(r) for r in z.view(np.uint32)]
Out[152]: ['0x7f800001L', '0x7fc00000L', '0x7f800000L', '0x3f800000L']
In [153]: issnan(z)
Out[153]: array([ True, False, False, False], dtype=bool)
There's bit manipulation answer that depends on how system-dependent you are willing to be. Quoting from wikipedia,
... most processors ... set the signaled/quiet bit to non-zero if the NaN is quiet, and to zero if the NaN is signaling. Thus, on these processors, the bit represents an 'is_quiet' flag.
The signaled/quiet bit is usually the first mantissa bit. So you can set a function to twiddle that bit as desired.

What does the perm_invK lemma in Ssreflect prove?

The following code is from perm.v in the Ssreflect Coq library.
I want to know what this result is.
Lemma perm_invK s : cancel (fun x => iinv (perm_onto s x)) s.
Proof. by move=> x /=; rewrite f_iinv. Qed.
Definitions in Ssreflect can involve lots of concepts, and sometimes it is hard to understand what is actually going on. Let's analyze this by parts.
iinv (defined in fintype.v) has type
iinv : forall (T : finType) (T' : eqType) (f : T -> T')
(A : pred T) (y : T'),
y \in [seq f x | x in A] -> T
What this does is to invert any function f : T -> T' whose restriction to a subdomain A \subset T is surjective on T'. Put in other words, if you give me an y that is in the list of results of applying f to all elements of A, then I can find you an x \in A such that f x = y. Notice that this relies crucially on the fact that T is a finite type and that T' has decidable equality. The correctness of iinv is stated in lemma f_iinv, which is used above.
perm_onto has type codom s =i predT, where s is some permutation defined on a finite type T. This is saying, as its name implies, that s is surjective (which is obvious, since it is injective, by the definition of permutations in perm.v, and by the fact that the domain and codomain are the same). Thus, fun x => iinv (perm_onto s x) is a function that maps an element x to an element y such that s y = x. In other words, its the inverse of s. perm_invK is simply stating that this function is indeed the inverse (to be more precise, it is saying that it is the left inverse of s).
The definition that is actually useful, however, is perm_inv, which appears right below. What it does is that it packages fun x => iinv (perm_onto s x) with its proof of correctness perm_invK to define an element of perm_inv s of type {perm T} such that perm_inv s * s = s * perm_inv s = 1. Thus, you can view it as saying that the type {perm T} is closed under inverses, which allows you to use a lot of the ssr machinery for e.g. finite groups and monoids.

Find regular intervals in a set of unordered numbers

I have a set of not-unique real numbers read from a file.
All these numbers were generated from a linear space, that is, the difference between numbers is always a multiple from a fixed value, the "step" or "grid size" of the linear space, so to say.
Each existing value will tipically appear many times in the file.
My goal would be to find how the values are spaced, so that I could put each (unique) value in an array and access its value with an index.
You are looking for the greatest common divisor of those numbers. Here it is in Python:
def gcd( a, b ):
"greatest common divisor"
while True:
c = a % b
if c < 1e-5:
return b
a, b = b, c
def gcdset( a_set ):
"use the pairwise gcd to find gcd of a set"
x = a_set.pop()
total = x
for u in a_set:
x = gcd( u, x )
# the following step is optional,
# some sort of stabilization just for improved accuracy
total = total + u
x = total / round(total/x)
return x
# the list where we want to find the gcd
inputlist = [2239.864226650253, 1250.4096410911607, 1590.1948696485413,
810.0479848807954, 2177.343744595695, 54.3656365691809, 2033.2748076873656,
2074.049035114251, 108.7312731383618, 2188.216871909531]
# we turn it into a set to get rid of duplicates
aset = set(inputlist)
print(gcdset( aset ))
If you don't have Python around you can play with this code here: http://ideone.com/N9xDWA

Resources