unit uzadehlogic;
{$mode objfpc}
{ Zadeh standard fuzzy logic operators
See:
https://commons.wikimedia.org/wiki/Fuzzy_operator
Enjoy,
Thaddy }
interface
type
{ Fuzz is a double in the inclusive range 0..1
The programmer is responsible for valid input.
(You can use EnsureRange from the math unit.) }
Fuzz = type double;
// AND
operator and (const a,b:Fuzz):Fuzz;inline;
// OR
operator or (const a,b:Fuzz):Fuzz;inline;
// NOT
operator not (const a:Fuzz):Fuzz;inline;
// XOR
operator xor (const a,b:Fuzz):Fuzz;inline;
{
* The following logic implemented as functions
* because Pascal doesn't know them as operators
}
// IMP
function imp(const a,b:Fuzz):Fuzz;inline;
// NAND
function nand(const a,b:Fuzz):Fuzz;inline;
// NOR
function nor(const a,b:Fuzz):Fuzz;inline;
// NXR
function nxr(const a,b:Fuzz):Fuzz;inline;
// NIMP
function nimp(const a,b:Fuzz):Fuzz;inline;
{ Utilities }
function min(const a,b:Fuzz):Fuzz;
function max(const a,b:Fuzz):Fuzz;
// to prevent overshoot. Not strictly necessary
function Limit(const a:Double):Fuzz;
implementation
function min(const a,b:Fuzz):Fuzz;
begin
if a > b then result := b else result := a;
end;
function max(const a,b:Fuzz):Fuzz;
begin
if b > a then result := b else result := a;
end;
function Limit(const a:Double):Fuzz;
begin
Result := min(1,max(0,a));
end;
// AND
operator and (const a,b:Fuzz):Fuzz;inline;
begin
Result:=min(limit(a),Limit(b));
end;
// OR
operator or (const a,b:Fuzz):Fuzz;inline;
begin
Result:=max(Limit(a),limit(b));
end;
// NOT
operator not (const a:Fuzz):Fuzz;inline;
begin
result := 1 - Limit(a);
end;
// XOR
operator xor (const a,b:Fuzz):Fuzz;inline;
begin
result := limit(a) + limit(b) - 2 * (a and b);
end;
// IMP
function imp(const a,b:Fuzz):Fuzz;inline;
begin
Result := not (a and not b)
end;
// NAND
function nand(const a,b:Fuzz):Fuzz;inline;
begin
Result := not (a and b);
end;
// NOR
function nor(const a,b:Fuzz):Fuzz;inline;
begin
Result := not (a or b);
end;
// NXR
function nxr(const a,b:Fuzz):Fuzz;inline;
begin
Result := not (a xor b);
end;
// NIMP
function nimp(const a,b:Fuzz):Fuzz;inline;
begin
Result := a and not b;
end;
end.