Generate Permutations using Hunspell affix rules - hunspell

The bounty expires in 2 days. Answers to this question are eligible for a +50 reputation bounty.
shantanuo wants to draw more attention to this question.
let's assure there is a new language where a combination of 3 letters in any order are valid. For e.g. abc, acb, bac, bca, cba, bab all are valid. How do I write a hunspell affix file for this?
affix file:
PFX A Y 1
PFX A 0 a .
PFX B Y 1
PFX B 0 b .
PFX C Y 1
PFX C 0 c .
SFX a Y 1
SFX a 0 a .
SFX b Y 1
SFX b 0 b .
SFX c Y 1
SFX c 0 c .
This is just an example assuming there are only 3 characters (ABC) but it can be extended to A to Z.
Dict file:
a/BCbc
b/ACac
c/ABab
Is there any other better way to write this code or this is the only way to achieve this?

Related

How to deduce left-hand side matrix from vector?

Suppose I have the following script, which constructs a symbolic array, A_known, and a symbolic vector x, and performs a matrix multiplication.
clc; clearvars
try
pkg load symbolic
catch
error('Symbolic package not available!');
end
syms V_l k s0 s_mean
N = 3;
% Generate left-hand-side square matrix
A_known = sym(zeros(N));
for hI = 1:N
A_known(hI, 1:hI) = exp(-(hI:-1:1)*k);
end
A_known = A_known./V_l;
% Generate x vector
x = sym('x', [N 1]);
x(1) = x(1) + s0*V_l;
% Matrix multiplication to give b vector
b = A_known*x
Suppose A_known was actually unknown. Is there a way to deduce it from b and x? If so, how?
Til now, I only had the case where x was unknown, which normally can be solved via x = b \ A.
Mathematically, it is possible to get a solution, but it actually has infinite solutions.
Example
A = magic(5);
x = (1:5)';
b = A*x;
A_sol = b*pinv(x);
which has
>> A
A =
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
but solves A as A_sol like
>> A_sol
A_sol =
3.1818 6.3636 9.5455 12.7273 15.9091
3.4545 6.9091 10.3636 13.8182 17.2727
4.4545 8.9091 13.3636 17.8182 22.2727
3.4545 6.9091 10.3636 13.8182 17.2727
3.1818 6.3636 9.5455 12.7273 15.9091

Consider the 4-to-1 multiplexer shown below. What values must inputs A, B, C, D be so that the multiplexer implements the function

I am completing some practice quizzes online in preperation for a uni exam next week, and I am a bit stumped as to how to solve this problem:
Consider the 4-to-1 multiplexer shown below. What values must inputs
A, B, C, D be so that the multiplexer implements the function
If anyone would'nt mind giving me a few tips as to how to solve this, I would much appreciate it.
Thanks
Corey
I'll assume: X = S1 * ~S0 + S0 * ~S1 as your posted picture is unreadable.
First you need to know the multiplexor transfer:
S1 S0 X
0 0 A
0 1 B
1 0 D
1 1 C
Then, apply the function to all your possible input combinations.
f(s1,s0)=S1*~S0+~S1*S0
f(0,0) = 0
f(0,1) = 1
f(1,0) = 1
f(1,1) = 0
And finally fill the table with X=f(s1,s0)
S1 S0 X f(s1,s0)
0 0 A 0
0 1 B 1
1 0 D 1
1 1 C 0

Boolean Expression for 4 input Logic gates

I have 4 inputs; (A, B, C, D) and 3 outputs; (X,Y,Z).
1)X is true when the input is less than 0111.
2)Y is true when the input is greater than 0111.
3)Z is true when the input is 0111.
Can someone help me out with the Boolean Expression for X?
I have already obtained the expressions for Y and Z which are as follows:
Y = A
_
Z = A . (B . C . D)
X is true when neither Y or Z are true:
_ _
X = Y + Z
or
_____
X = Y . Z
The expansion of which can be simplified, hint:
_ _ _
A + A = A
From first principles, any expression can be obtained from the truth table by OR'ing the true AND expression for each row that has a true result (then simplifying where possible); for example:
A B C X
--------- _ _ _
0 0 0 1 = A . B . C
0 0 1 0
0 1 0 0
0 1 1 0
1 0 0 0
1 0 1 0
1 1 0 0
1 1 1 1 = A . B . C
_ _ _
X = (A . B . C) + (A . B . C)
alternatively:
_________
X = (A + B + C) + (A . B . C)
For large truth tables, this can become cumbersome (which is why my example has only three variables), in these cases a Karnaugh Map could be used instead.

Iterating through matrix rows in Octave without using an index or for loop

I am trying to understand if it's possible to use Octave more efficiently by removing the for loop I'm using to calculate a formula on each row of a matrix X:
myscalar = 0
for i = 1:size(X, 1),
myscalar += X(i, :) * y(i) % y is a vector of dimension size(X, 1)
...
The formula is more complicate than adding to a scalar. The question here is really how to iterate through X rows without an index, so that I can eliminate the for loop.
Yes, you can use broadcasting for this (you will need 3.6.0 or later). If you know python, this is the same (an explanation from python). Simply multiply the matrix by the column. Finnaly, cumsum does the addition but we only want the last row.
newx = X .* y;
myscalars = cumsum (newx, 1) (end,:);
or in one line without temp variables
myscalars = cumsum (X .* y, 1) (end,:);
If the sizes are right, broadcasting is automatically performed. For example:
octave> a = [ 1 2 3
1 2 3
1 2 3];
octave> b = [ 1 0 2];
octave> a .* b'
warning: product: automatic broadcasting operation applied
ans =
1 0 6
1 0 6
1 0 6
octave> a .* b
warning: product: automatic broadcasting operation applied
ans =
1 2 3
0 0 0
2 4 6
The reason for the warning is that it's a new feature that may confuse users and is not existent in Matlab. You can turn it off permanentely by adding warning ("off", "Octave:broadcast") to your .octaverc file
For anyone using an older version of Octave, the same can be accomplished by calling bsxfun directly.
myscalars = cumsum (bsxfun (#times, X, y), 1) (end,:);

Convert byte to specific mask with bit hack

I have number with binary representation 0000abcd.
How convert it to 0a0b0c0d with smallest number of operations?
How convert 0a0b0c0d back to 0000abcd?
I was searching for a solution here:
http://graphics.stanford.edu/~seander/bithacks.html and other
Generally the problem a bit more than described.
Given first number a₁b₁c₁d₁a₂b₂c₂d₂ and second number a₃a₄b₃b₄c₃c₄d₃d₄
If (a₁ and a₂ = 0) then clear both a₃ and a₄, if (a₃ and a₄ = 0) then clear both a₁ and a₂, etc.
My solution:
a₁b₁c₁d₁a₂b₂c₂d₂
OR 0 0 0 0 a₁b₁c₁d₁ ( a₁b₁c₁d₁a₂b₂c₂d₂ >> 4)
----------------
0 0 0 0 a b c d
? (magic transformation)
? ? ? ? ? ? ? ?
----------------
0 a 0 b 0 c 0 d
OR a 0 b 0 c 0 d 0 (0 a 0 b 0 c 0 d << 1)
----------------
a a b b c c d d
AND a₃a₄b₃b₄c₃c₄d₃d₄
----------------
A₃A₄B₃B₄C₃C₄D₃D₄ (clear bits)
UPDATED: (thanks for #AShelly)
x = a₁b₁c₁d₁a₂b₂c₂d₂
x = (x | x >> 4) & 0x0F
x = (x | x << 2) & 0x33
x = (x | x << 1) & 0x55
x = (x | x << 1)
y = a₃a₄b₃b₄c₃c₄d₃d₄
y = (y | y >> 1) & 0x55
y = (y | y >> 1) & 0x33
y = (y | y >> 2) & 0x0F
y = (y | y << 4)
work for 32-bit with constants 0x0F0F0F0F, 0x33333333, 0x55555555 (and twice long for 64-bit).
If you're looking for the smallest number of operations, use a look-up table.
I have number with binary
representation 0000abcd. How convert
it to 0a0b0c0d with smallest number of
operations?
Isn't this exactly "Interleave bits of X and Y" where Y is 0? Bit Twiddling Hacks has Multiple Solutions that don't use a lookup table.
How convert 0a0b0c0d back to 0000abcd?
See "How to de-interleave bits (UnMortonizing?)"
You can't do it in one go, you should shift bits on per bit basis:
Pseudo code:
X1 = a₁b₁c₁d₁
X2 = a₂b₂c₂d₂
Bm = 1 0 0 0 // Bit mask
Result = 0;
while (/* some bytes left */)
{
Result += (X1 and Bm) << 1 or (X2 and Bm);
Bm = Bm shr 1
Result = Result shl 2;
}
As a result you will get a1a2b1b2c1c2d1d2
I think it is not possible (without lookup table) to do it in less operations using binary arithmetic and x86 or x64 processor architecture. Correct me if I'm mistaken but your problem is about moving bits. Having the abcd bits you want to get 0a0b0c0d bits in one operation. The problem starts when you will look at how many bits the 'a','b','c' and 'd' has to travel.
'a' was 4-th, became 7-th, distance travelled 3 bits
'b' was 3-rd, became 5-th, distance travelled 2 bits
'c' was 2-nd, became 3-rd, distance travelled 1 bit
'd' was 1-st, became 1-st, distance travelled 0 bits
There is no such processor instruction that will move these bits dynamically to a different distance. Though if you have different input representations of the same number for free, for example you have precomputed several values which you are using in a cycle, than maybe it will be possible to gain some optimization, this is the effect you get when using additional knowledge about the topology. You just have to choose whether it will be:
[4 cycles, n^0 memory]
[2 cycles, n^1 memory]
[1 cycle , n^2 memory]