How to Realize a Diff Function

How to realize a diff function?

You have here a javascript example of the implementation of a diff algorithm.

Based on:

P. Heckel, A technique for isolating differences between files
Comm. ACM, 21, (4), 264--268 (1978).

The implementation, itself, has two functions, one of which is recommended for use:

diffString( String oldFile, String newFile )

This method takes two strings and calculates the differences in each. The final result is the 'newFile' marked up with HTML (to signify both deletions from the oldFile and additions to the newFile).

How to use Maxima's diff in function definition?

Michael's answer is good, but it does the differentiation everytime g(x) is called. (Also, normally you see it wrapped in a block statement to ensure that y is properly localized).

There is a way to force the RHS to evaluate at the time of definition
and with the general x.

The syntax is

(%i1) f(x) := 2*x^4;
4
(%o1) f(x) := 2 x
(%i2) g(x) := ''(diff(f(x), x) - 8);
3
(%o2) g(x) := 8 x - 8
(%i3) g(0);
(%o3) - 8

Compare with the block construct:

(%i4) h(x) := block([y], subst([y = x], diff(f(y), y) - 8));
(%o4) h(x) := block([y], subst([y = x], diff(f(y), y) - 8))
(%i5) h(0);
(%o5) - 8

Notice (%o4) which shows that the RHS is unevaluated.

Ref: http://www.math.utexas.edu/pipermail/maxima/2007/004706.html

How to perform string Diffs in Java?

This library seems to do the trick: google-diff-match-patch. It can create a patch string from differences and allow to reapply the patch.

edit: Another solution might be to https://code.google.com/p/java-diff-utils/

How to get the difference between two arrays in JavaScript?

This answer was written in 2009, so it is a bit outdated, also it's rather educational for understanding the problem. Best solution I'd use today would be

let difference = arr1.filter(x => !arr2.includes(x));

(credits to other author here)

I assume you are comparing a normal array. If not, you need to change the for loop to a for .. in loop.

function arr_diff (a1, a2) {

var a = [], diff = [];

for (var i = 0; i < a1.length; i++) {
a[a1[i]] = true;
}

for (var i = 0; i < a2.length; i++) {
if (a[a2[i]]) {
delete a[a2[i]];
} else {
a[a2[i]] = true;
}
}

for (var k in a) {
diff.push(k);
}

return diff;
}

console.log(arr_diff(['a', 'b'], ['a', 'b', 'c', 'd']));
console.log(arr_diff("abcd", "abcde"));
console.log(arr_diff("zxc", "zxc"));

Pandas' diff but user defined function

Here's a way that works for me using pandas DataFrame built-in method 'apply'.

I wrapped the custom diff function, product, which takes two arguments and returns one value, in diff_custom which takes a vector argument and returns an equal length vector, appropriately NaN-padded. We execute diff_custom using the the pandas DataFrame's built-in apply method:

import pandas
import numpy

df = pandas.DataFrame([[4, 9],[5,10],[22,44]], columns=['A', 'B'])

# Function that acts on neighboring row or column values, val1 and val2
def product(val1,val2):
return(val1*val2)

# Function that acts on DataFrame row or column, x
def diff_custom(x):
vals = [product(x[n],x[n+1]) for n in range(x.shape[0]-1)]
ret = list([numpy.nan]) # pad return vector however you need to
ret = ret + vals
return(ret)

# Use DataFrame built-in 'apply' method
df.apply(diff_custom,axis=1)
A B
0 NaN 36.0
1 NaN 50.0
2 NaN 968.0

df.apply(diff_custom,axis=0)
A B
0 NaN NaN
1 20.0 90.0
2 110.0 440.0

What is the Windows equivalent of the diff command?

Run this in the CMD shell or batch file:

FC file1 file2

FC can also be used to compare binary files:

FC /B file1 file2

how to implement diff with perl

You can use the CPAN module Algorithm::Diff for computing the difference between two files or lists.



Related Topics



Leave a reply



Submit