Using AND or OR logic gates to construct a 4-variable function - logic

I'm a software developer, and I'm trying to model a function using only AND or OR gates. I remember having similar subjects in my undergraduates, but I don't recall it. f(x,y,z,w) is a function of four variables, and gets True when AT LEAST two of the variables gets true. How can I visually construct it using only AND or OR gates?
UPDATE: I think f= xy+xz+xw+yz+yw+zw if I'm correct!

The following expression is logically what you want:
(xy) + (xz) + (xw) + (yz) + (yw) + (zw)
x (y + z + w) + y (z + w) + (zw)
Note that you don't need to check for cases of three or four TRUE values, because they are already included in the check for two TRUE values.
I represent AND gates with scalar multiplication, and OR gates using the addition operator (+). Note that when you wire up the actual circuit you might even be able to simplify even more than I have by reusing pieces (e.g. z + w).

Related

Combine boolean and integer logic in linear arithmetic using the Z3 Solver?

I would like to solve problems combining boolean and integer logic in linear arithmetic with a SAT/SMT solver. At first glance, Z3 seems promising.
First of all, is it at all possible to solve the following problem? This answer makes it seem like it works.
int x,y,z
boolean a,b,c
( (3x + y - 2z >= 10) OR (A AND (NOT B OR C)) OR ((A == C) AND (x + y >= 5)) )
If so, how does Z3 solve this kind of problem in theory and is there any documentation about it?
I could think of two ways to solve this problem. One would be to convert the Boolean operations into a linear integer expression. Another solution I read about is to use the Nelson-Oppen Combination Method described in [Kro 08].
I found a corresponding documentation in chapter 3.2.2. Solving Arithmetical Fragments, Table 1 a listing of the implemented algorithms for a certain logic.
Yes, SMT solvers are quite good at solving problems of this sort. Your problem can be expressed using z3's Python interface like this:
from z3 import *
x, y, z = Ints('x y z')
A, B, C = Bools('A B C')
solve (Or(3*x + y - 2*z >= 10
, And(A, Or(Not(B), C))
, And(A == C, x + y >= 5)))
This prints:
[A = True, z = 3, y = 0, B = True, C = True, x = 5]
giving you a (not necessarily "the") model that satisfies your constraints.
SMT solvers can deal with integers, machine words (i.e., bit-vectors), reals, along with many other data types, and there are efficient procedures for combinations of linear-integer-arithmetic, booleans, uninterpreted-functions, bit-vectors amongst many others.
See http://smtlib.cs.uiowa.edu for many resources on SMT solving, including references to other work. Any given solver (i.e., z3, yices, cvc etc.) will be a collection of various algorithms, heuristics and tactics. It's hard to compare them directly as each shine in their own way for certain sublogics, but for the base set of linear-integer arithmetic, booleans, and bit-vectors, they should all perform fairly well. Looks like you already found some good references, so you can do further reading as necessary; though for most end users it's neither necessary nor that important to know how an SMT solver internally works.

Differentiate an infix formal language functions

I have a source file like (without loss of generality (only to image a possible syntax)):
function a()
return g // global variable without any internal structure exactly
end
function b(x, y)
local z = x * y
return z + 1
end
function c(z, t)
return b(z * z, a())
end
// ...etc
I want to defferentiate any function WRT to some variable.
All the formal parametres we can treat as a functions with unknown at derive time internal structure.
If I stand correct further, then the following is truth (for depending symbols ' is part of symbol, for global variables is operator during substitute time stage (def: g{g} is one, but g{y} is zero)):
function a'()
return g';
end
function b'(x, y, x', y')
local z' = x' * y + x * y'
return z' + 0
end
But what to do with last function? Namely, with actual parameters in substitution of function b?
Is there any ready to use implementations of general algorithm to work with the above? What to do with higher order derivatives (especially interesting, how to handle the formal parameters)? Are there any other possible unclear cases?
I would suggest having your parameters be symbolic expressions that know how to respond to derivatives, and having all operations take functions and return functions. Then you will get a final expression that knows how to be represented as a derivative. Furthermore you can do things like partial derivatives at a later point because you have the symbolic expression.
For a real example of what I mean, see http://www.elem.com/~btilly/kelly-criterion/js/advanced-math.js for a library that I wrote to solve a calculus problem in JavaScript, and search for "Optimize if requested" in the source for http://www.elem.com/~btilly/kelly-criterion/betting-returns2.html to see how I used it. See http://www.elem.com/~btilly/kelly-criterion/ for an explanation of why I was writing that code.
In that example I, of course, was not working from infix notation. But that is a standard parsing problem that I think you know how to solve.

How to rearrange a function y = f[x] into x = g[y]

I have a differential equation A*dx/dt + B(y-y0) = 0
Where x is a very complicated function of y.
How can I use Mathematica to rearrange y to get a function x in order to solve this?
Thanks
There are two or three different problems here that you might be asking:
Option 1: The subject line
First, if you really do have a function f[x] defined and you want to rearrange it, you would be doing something like this:
f[x_]=2+x+x^2;
Solve[y==f[x],x]
However, even here you should notice that inverse functions are not necessarily unique. There are two functions given, and the domain of each is only for y>=7/4.
Option 2: Solving a DE
Now, the equation you give is a differential equation. That is not the same as "rearranging a function y=f[x] into x=g[y]" because there are derivatives involved.
Mathematica has a built-in differential-equation solver:
DSolve[a y'[t] + b (y[t] - y0) == 0, y[t], t]
That will give you a function (in terms of constants $a,b,y_0$) that is the answer, and it will include the unspecified constant of integration.
Your system seems to refer to two functions, x(t) and y(t). You cannot solve one equation for two variables, so it is impossible to solve this (Mathematica or otherwise) without more information.
Option 3: Rearranging an expression
As a third alternative, if you are trying to rearrange this equation without solving the differential equation, you can do that:
Solve[a x'[t] + b(y[t]-y0)==0,x'[t]]
This will give you $x'(t)$ in terms of the other constants and the function $y(t)$, but in order to integrate this (i.e. to solve the differential equation) you will need to know more about y[t].

using arithmetic operations in Prolog

I have the following code:
position(0,0).
move(f):-
position(X,Y),
number(X),
number(Y),
Y is Y+1,
X is X+1.
but when i call move(f) it returns false. number(X) and number(Y) returns true but whem i add the other two lines the function doesn't work. what's the problem?
Elaborating on some of the comments your question has received, variables in Prolog stand for a possible instantiation of a single value, just like variables in mathematics and mathematical logic, and once they are instantiated within a context they must remain consistent. If we're dealing with a formula 0 = (a + b) - (a + b), we know that it can only express its intended sense if any value assigned to the first a is also assigned to the second. That is, we can substitute any value for a, but it must be the same value throughout. Prolog works with variables in this same way. If x = x + 1, then 2 = 3; but then math would be broken.
Addressing mat's caution against using dynamic predicates, here is a possible way of handling moves, but accomplished by passing around a list of previous moves. With this method, the most recent move will always be the first element of List in the compound term moves(List).
Supposing the current history of moves is as follows:
moves([position(0,0), position(0,1), position(1,1)]).
move/3 takes a direction, a complex term representing the previous moves, and tells us what the updated list of moves is.
move(Direction, moves([From|Ms]), moves([To,From|Ms])) :-
move_in_direction(Direction,From,To).
move_in_direction/3 takes a direction, and a position, and tells us what the next position in that direction is:
move_in_direction(left, position(X1,Y1), position(X2,Y1)) :- X2 is X1 - 1.
move_in_direction(right, position(X1,Y1), position(X2,Y1)) :- X2 is X1 + 1.
move_in_direction(up, position(X1,Y1), position(X1,Y2)) :- Y2 is Y1 + 1.
move_in_direction(down, position(X1,Y1), position(X1,Y2)) :- Y2 is Y1 - 1.
Notice that, using this method, you get a back-trackable history of moves for free. I'd imagine you could use this in interesting ways -- e.g. having the player explore possible series of moves until a certain condition is met, at which point it commits or backtracks. I'd be interested to know what kind of solution you end up going with.

Can I Make NOT Using OR and AND?

I know i can invert X using NOT.
NOT x = x'
But, can i invert X just with OR & AND ?
Example
Given this function
F = W'.Y.Z' + V.W'.Z'
Can i make a circuit just with OR & AND ?
Thanks
It is not possible to make NOT out of AND and OR. The first obvious reason is that NOT takes only one argument, while both AND and OR take two. Even if you feed the same variable twice to the AND/OR gates, they will not invert its value
OTOH, you can define AND in terms of OR+NOT and you can define OR in terms of AND+NOT
x AND y = NOT((NOT x) OR (NOT y))
x OR y = NOT((NOT x) AND (NOT y))
You can't get NOT from OR and AND. Proof:
With a 0 input, OR and AND will both be 0. There will be no 1 anywhere in the system. With a 1 input, OR and AND will both be 1. There will be no 0 anywhere.
This is why NAND and NOR chips are popular for small/hobby electronics, since they can make any other logic combination.
No, you can't get NOT with just combinations of ANDs and ORs.

Resources