# Function diff #

Create a new matrix or array of the difference between elements of the given array The optional dim parameter lets you specify the dimension to evaluate the difference of If no dimension parameter is passed it is assumed as dimension 0

Dimension is zero-based in javascript and one-based in the parser and can be a number or bignumber Arrays must be ‘rectangular’ meaning arrays like [1, 2] If something is passed as a matrix it will be returned as a matrix but other than that all matrices are converted to arrays

## Syntax #

``````math.diff(arr)
math.diff(arr, dim)
``````

### Parameters #

Parameter Type Description
`arr` Array | Matrix An array or matrix
`dim` number Dimension

### Returns #

Type Description
Array | Matrix Difference between array elements in given dimension

### Throws #

Type | Description —- | ———–

## Examples #

``````const arr = [1, 2, 4, 7, 0]
math.diff(arr) // returns [1, 2, 3, -7] (no dimension passed so 0 is assumed)
math.diff(math.matrix(arr)) // returns Matrix [1, 2, 3, -7]

const arr = [[1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [9, 8, 7, 6, 4]]
math.diff(arr) // returns [[0, 0, 0, 0, 0], [8, 6, 4, 2, -1]]
math.diff(arr, 0) // returns [[0, 0, 0, 0, 0], [8, 6, 4, 2, -1]]
math.diff(arr, 1) // returns [[1, 1, 1, 1], [1, 1, 1, 1], [-1, -1, -1, -2]]
math.diff(arr, math.bignumber(1)) // returns [[1, 1, 1, 1], [1, 1, 1, 1], [-1, -1, -1, -2]]

math.diff(arr, 2) // throws RangeError as arr is 2 dimensional not 3
math.diff(arr, -1) // throws RangeError as negative dimensions are not allowed

// These will all produce the same result
math.diff([[1, 2], [3, 4]])
math.diff([math.matrix([1, 2]), math.matrix([3, 4])])
math.diff([[1, 2], math.matrix([3, 4])])
math.diff([math.matrix([1, 2]), [3, 4]])
// They do not produce the same result as  math.diff(math.matrix([[1, 2], [3, 4]])) as this returns a matrix
``````
{"ft_lang_label":"__label__en","ft_lang_prob":0.5303243,"math_prob":0.99874574,"size":1819,"snap":"2022-40-2023-06","text_gpt3_token_len":621,"char_repetition_ratio":0.17575757,"word_repetition_ratio":0.12944984,"special_character_ratio":0.39087412,"punctuation_ratio":0.24489796,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99706185,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-02-02T23:51:25Z\",\"WARC-Record-ID\":\"<urn:uuid:9a9aa136-ddde-4ed9-96c6-67aaee81ad12>\",\"Content-Length\":\"12832\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:fe0ce9c4-5492-45df-a467-2e41d85fc955>\",\"WARC-Concurrent-To\":\"<urn:uuid:31fef648-2c08-428d-85df-cc48ccf0b87d>\",\"WARC-IP-Address\":\"185.199.108.153\",\"WARC-Target-URI\":\"https://mathjs.org/docs/reference/functions/diff.html\",\"WARC-Payload-Digest\":\"sha1:YMI3UUP3S74CDGGPEZ7724D3HSR74OVG\",\"WARC-Block-Digest\":\"sha1:TTFHVTQYG2I427QK4BIEAYWAYRTPO2GA\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-06/CC-MAIN-2023-06_segments_1674764500041.2_warc_CC-MAIN-20230202232251-20230203022251-00816.warc.gz\"}"}