git.iko.soy/ikoHSE/SolveAvdoshin
on master help sign in
a unified API for derivatives
commit066b72111e20c2d72611c99cc2204843102c4c00 authorIlya Gulkov <ifranked@gmail.com> dateSun 4 Oct 2015, 17:16 (11 years ago) parente93c60e
1 file changed+51−76
SolveAvdoshin/BooleanFunctions.cs+51−76
SolveAvdoshin/BooleanFunctions.cs+51−76
@@ -149,25 +149,27 @@ namespace SolveAvdoshin
149149 Console.WriteLine(f.ToString());
150150
151151 Console.WriteLine();
152− Console.WriteLine(f.DerivA().ToString("F'_A"));
153− Console.WriteLine(f.DerivB().ToString("F'_B"));
154− Console.WriteLine(f.DerivC().ToString("F'_C"));
152+ Console.WriteLine(f.Deriv(BooleanVariable.A).ToString("F'_A"));
153+ Console.WriteLine(f.Deriv(BooleanVariable.B).ToString("F'_B"));
154+ Console.WriteLine(f.Deriv(BooleanVariable.C).ToString("F'_C"));
155155
156156 Console.WriteLine();
157− Console.WriteLine(f.DerivA().DerivB().ToString("F''_AB"));
158− Console.WriteLine(f.DerivB().DerivC().ToString("F''_BC"));
159− Console.WriteLine(f.DerivC().DerivA().ToString("F''_AC"));
157+ Console.WriteLine(f.Deriv(BooleanVariable.A, BooleanVariable.B).ToString("F''_AB"));
158+ Console.WriteLine(f.Deriv(BooleanVariable.B, BooleanVariable.C).ToString("F''_BC"));
159+ Console.WriteLine(f.Deriv(BooleanVariable.A, BooleanVariable.C).ToString("F''_AC"));
160160
161161 Console.WriteLine();
162− Console.WriteLine(f.DerivA().DerivB().DerivC().ToString("F'''_ABC"));
162+ Console.WriteLine(f.Deriv(BooleanVariable.A, BooleanVariable.B, BooleanVariable.C)
163+ .ToString("F'''_ABC"));
163164
164165 Console.WriteLine();
165− Console.WriteLine(f.DerivAB().ToString("F'_(A,B)"));
166− Console.WriteLine(f.DerivBC().ToString("F'_(B,C)"));
167− Console.WriteLine(f.DerivAC().ToString("F'_(A,C)"));
166+ Console.WriteLine(f.DirectionalDeriv(BooleanVariable.A, BooleanVariable.B).ToString("F'_(A,B)"));
167+ Console.WriteLine(f.DirectionalDeriv(BooleanVariable.B, BooleanVariable.C).ToString("F'_(B,C)"));
168+ Console.WriteLine(f.DirectionalDeriv(BooleanVariable.A, BooleanVariable.C).ToString("F'_(A,C)"));
168169
169170 Console.WriteLine();
170− Console.WriteLine(f.DerivABC().ToString("F'_(A,B,C)"));
171+ Console.WriteLine(f.DirectionalDeriv(BooleanVariable.A, BooleanVariable.B, BooleanVariable.C)
172+ .ToString("F'_(A,B,C)"));
171173 }
172174 }
173175
@@ -210,94 +212,67 @@ namespace SolveAvdoshin
210212 return caption.PadRight(10) + " " + String.Join(" ", digits);
211213 }
212214
213− public BooleanFunction DerivAB()
215+ public BooleanFunction DirectionalDeriv(params BooleanVariable[] variables)
214216 {
217+ Dictionary<BooleanVariable, bool> values;
218+
215219 int res = 0;
216220
217221 for(int i = 0; i < 8; i++) {
218222 res *= 2;
219223
220− bool a = i / 4 == 1;
221− bool b = i / 2 % 2 == 1;
222−
223− res += FixA(a ? 1 : 0).FixB(b ? 1 : 0).EvalAt(i) ^ FixA(a ? 0 : 1).FixB(b ? 0 : 1).EvalAt(i);
224− }
225−
226− return new BooleanFunction((byte)res);
227− }
228−
229− public BooleanFunction DerivBC()
230− {
231− int res = 0;
224+ values = new Dictionary<BooleanVariable, bool> { { BooleanVariable.A, i / 4 == 1 },
225+ { BooleanVariable.B, i / 2 % 2 == 1 },
226+ { BooleanVariable.C, i % 2 == 1 },
227+ };
232228
233− for(int i = 0; i < 8; i++) {
234− res *= 2;
229+ BooleanFunction leftOp = this;
230+ BooleanFunction rightOp = this;
235231
236− bool b = i / 2 % 2 == 1;
237− bool c = i % 2 == 1;
232+ foreach(var variable in variables) {
233+ leftOp = leftOp.FixVar(variable, values[variable] ? 1 : 0);
234+ rightOp = rightOp.FixVar(variable, values[variable] ? 0 : 1);
235+ }
238236
239− res += FixB(b ? 1 : 0).FixC(c ? 1 : 0).EvalAt(i) ^ FixB(b ? 0 : 1).FixC(c ? 0 : 1).EvalAt(i);
237+ res += leftOp.EvalAt(i) ^ rightOp.EvalAt(i);
240238 }
241239
242240 return new BooleanFunction((byte)res);
243241 }
244242
245− public BooleanFunction DerivAC()
243+ public BooleanFunction Deriv(params BooleanVariable[] variables)
246244 {
247− int res = 0;
248−
249− for(int i = 0; i < 8; i++) {
250− res *= 2;
251−
252− bool a = i / 4 == 1;
253− bool c = i % 2 == 1;
245+ BooleanFunction res = this;
254246
255− res += FixA(a ? 1 : 0).FixC(c ? 1 : 0).EvalAt(i) ^ FixA(a ? 0 : 1).FixC(c ? 0 : 1).EvalAt(i);
247+ foreach(var variable in variables) {
248+ res = new BooleanFunction((byte)(res.FixVar(variable, 0).Eval() ^ res.FixVar(variable, 1).Eval()));
256249 }
257250
258− return new BooleanFunction((byte)res);
251+ return res;
259252 }
260253
261− public BooleanFunction DerivABC()
254+ BooleanFunction FixVar(BooleanVariable variable, int value)
262255 {
263256 int res = 0;
264257
265− for(int i = 0; i < 8; i++) {
266− res *= 2;
267−
268− bool a = i / 4 == 1;
269− bool b = i / 2 % 2 == 1;
270− bool c = i % 2 == 1;
271−
272− res += FixA(a ? 1 : 0).FixB(b ? 1 : 0).FixC(c ? 1 : 0).EvalAt(i) ^
273− FixA(a ? 0 : 1).FixB(b ? 0 : 1).FixC(c ? 0 : 1).EvalAt(i);
258+ switch(variable) {
259+ case BooleanVariable.A:
260+ res = FixA(value);
261+ break;
262+ case BooleanVariable.B:
263+ res = FixB(value);
264+ break;
265+ case BooleanVariable.C:
266+ res = FixC(value);
267+ break;
268+ default:
269+ throw new ArgumentException("The variable argument for FixVar should be A, B or C");
274270 }
275271
276272 return new BooleanFunction((byte)res);
277273 }
278274
279− public BooleanFunction DerivA()
280− {
281− byte res = (byte)(FixA(0).Eval() ^ FixA(1).Eval());
282−
283− return new BooleanFunction(res);
284− }
285−
286− public BooleanFunction DerivB()
287− {
288− byte res = (byte)(FixB(0).Eval() ^ FixB(1).Eval());
289−
290− return new BooleanFunction(res);
291− }
292−
293− public BooleanFunction DerivC()
294− {
295− byte res = (byte)(FixC(0).Eval() ^ FixC(1).Eval());
296−
297− return new BooleanFunction(res);
298− }
299−
300− BooleanFunction FixA(int value)
275+ byte FixA(int value)
301276 {
302277 int res = 0;
303278
@@ -313,10 +288,10 @@ namespace SolveAvdoshin
313288 throw new ArgumentException("Value for Fix[Variable] should be either 0 or 1");
314289 }
315290
316− return new BooleanFunction((byte)res);
291+ return (byte)res;
317292 }
318293
319− BooleanFunction FixB(int value)
294+ byte FixB(int value)
320295 {
321296 int res = 0;
322297
@@ -336,10 +311,10 @@ namespace SolveAvdoshin
336311 throw new ArgumentException("Value for Fix[Variable] should be either 0 or 1");
337312 }
338313
339− return new BooleanFunction((byte)res);
314+ return (byte)res;
340315 }
341316
342− BooleanFunction FixC(int value)
317+ byte FixC(int value)
343318 {
344319 int res = 0;
345320
@@ -367,7 +342,7 @@ namespace SolveAvdoshin
367342 throw new ArgumentException("Value for Fix[Variable] should be either 0 or 1");
368343 }
369344
370− return new BooleanFunction((byte)res);
345+ return (byte)res;
371346 }
372347 }
373348