Finite state machine that accepts if sum of digits divisible by 3 .
I am trying to construct a finite state machine the accepts if the sum of digits is divisible by n. So far I was able to do for n=2 and n=3 but dint find any generalized steps that I could follow. Any help is appreciated.
This question is a little vague but it seems like you are trying to accept a stream of numbers if they are divisible by n.
If this is the case I would suggest you gathering input, separating by digit, summing the digits and using a mod. Some clarification would help my answer though.
It seems like your alphabet is ternary and that it consists of 0, 1, and 2. For any n, you must have an n-state machine with each state representing the remainder when dividing by n. The transition for any x equal to 0, 1, or 2 from state z will go to state (z+x)%n where "%" represents the remainder operator.
Related
From what I understand, in mathematics, the mod operator is the result of the remainder of Euclidean division. Where 0 ≤ r < |b|, meaning that the result will always be positive.
In programming however, there are operators in many languages which can be used to mean either the remainder operator or modulo operator which differ with respect to how they handle negative values.
(I believe that mod operator in math, remainder operator in programming, and mod operator in programming yield the same results for positive numbers)
According to Modulo operation with negative numbers
"With a remainder operator, the sign of the result is the same as the
sign of the dividend while with a modulo operator the sign of the
result is the same as the divisor."
So the mod operator in programming is not referring to the mod operator in math?
Is the sign of the answer the main distinguishing factor between mod operator vs remainder operator in programming?
Mathematically, the modulus is part of group theory, and the idea of a set. You can generate all the numbers in a set by addition, with the modulus. So if your set is integers modulus 10, you count 0-9 and then start over at 0. There is no concept of a negative number under modulus.
In programming, the remainder is what portion is left after doing a division. So if you divide 3 by 10, you're left with 0 and a remainder of 3. If you divide -3 by 10, you get 0 with a remainder of -3, rather than -1 remainder 7. But the mathematical modulus is 7.
Those who designed integer division that we now use decided that it's more logical to round towards 0, rather than round towards negative infinity, so by necessity, a negative division will result in a negative remainder.
If you want to convert a remainder to a modulus, you need to add your modulus to any negative remainders in order to map them into the proper range.
Given a bit vector in Z3, I am wondering how can I sum up each individual bit of this vector?
E.g.,
a = BitVecVal(3, 2)
sum_all_bit(a) = 2
Is there any pre-implemented APIs/functions that support this? Thank you!
It isn't part of the bit-vector operations.
You can create an expression as follows:
def sub(b):
n = b.size()
bits = [ Extract(i, i, b) for i in range(n) ]
bvs = [ Concat(BitVecVal(0, n - 1), b) for b in bits ]
nb = reduce(lambda a, b: a + b, bvs)
return nb
print sub(BitVecVal(4,7))
Of course, log(n) bits for the result will suffice if you prefer.
The page:
https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetNaive
has various algorithms for counting the bits; which can be translated to Z3/Python with relative ease, I suppose.
My favorite is: https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetKernighan
which has the nice property that it loops as many times as there are set bits in the input. (But you shouldn't extrapolate from that to any meaningful complexity metric, as you do arithmetic in each loop, which might be costly. The same is true for all these algorithms.)
Having said that, if your input is fully symbolic, you can't really beat the simple iterative algorithm, as you can't short-cut the iteration count. Above methods might work faster if the input has concrete bits.
So you're computing the Hamming Weight of a bit vector. Based on a previous question I had, one of the developers had this answer. Based on that original answer, this is how I do it today:
def HW(bvec):
return Sum([ ZeroExt(int(ceil(log2(bvec.size()))), Extract(i,i,bvec)) for i in range(bvec.size())])
I have learned in some essays (Tomas Mikolov...) that a better way of forming the vector for a sentence is to concatenate the word-vector.
but due to my clumsy in mathematics, I am still not sure about the details.
for example,
supposing that the dimension of word vector is m; and that a sentence has n words.
what will be the correct result of concatenating operation?
is it a row vector of 1 x m*n ? or a matrix of m x n ?
There are at least three common ways to combine embedding vectors; (a) summing, (b) summing & averaging or (c) concatenating. So in your case, with concatenating, that would give you a 1 x m*a vector, where a is the number of sentences. In the other cases, the vector length stays the same. See gensim.models.doc2vec.Doc2Vec, dm_concat and dm_mean - it allows you to use any of those three options [1,2].
[1] http://radimrehurek.com/gensim/models/doc2vec.html#gensim.models.doc2vec.LabeledLineSentence
[2] https://github.com/piskvorky/gensim/blob/develop/gensim/models/doc2vec.py
To generate a hash function, Map a key k into one of m slots by taking the remainder of k divided by m. That is, the hash function is
h(k) = k mod m.
I have read at several places that a good choice of m will be
A prime - I understand that we want to remove common factors, hence a prime number is chosen
not too close to an exact power of 2 - why is that?
From Introduction to algorithms :
When using the division method we avoid certain values of m. For
example m should not be power of 2. Since if m=2^p then h(k) is p
lowest-order bits of k. Unless it is known that all low-order p-bit
patterns are equally likely,
it is better to make a hash function
depend on all bits of the key.
As you se from the below image if i chose 2^3 which mean p=3 and m=8. The hashed keys are only dependent to lowest 3(p) bits which is bad because when you hash you want to include as much data as possible for a good distribution.
Let's assume we are given N1 and N2: q-bit length 2 binary numbers. For simplicity, both are unsigned integers. Can we express multiplication or addition of N1 and N2 in terms of the number of bit-wise operations like AND, OR, XOR, SHIFT needed to perform this operation??? Reasonable estimation would also be fine.
Every information, thoughts, links are highly appreciated.
Thanks!