# Inverses

## Inverse and Obverse

The inverse of a function f is a function that undoes the effect of f.

The obverse of a verb u is usually the inverse of u, except that you may assign an obverse using Obverse (u :. v).

The obverse of v is executed when you use v^:_1 or u&.v or u&.:v.

You can see the obverse of u by executing u b. _1

```   +/\ 2 1 1 1         NB. +/\ = running total
2 3 4 5
+/\ b. _1           NB. You can see its inverse
(- |.!.0) :.(+/\)
+/\^:_1 (2 3 4 5)   NB. It works!
2 1 1 1
```

Most obverses work only for the monadic valence

```   2 +/\ 2 3 4 5
5 7 9
2 +/\^:_1 (5 7 9)
|domain error
|   2    +/\^:_1(5 7 9)
```

Inverses may be defined for the dyadic valence, but you have to find these by trial and error.

## Predefined Obverses

### Obverses Of Primitives

The obverses of primitives are shown in the following table

 Predefined Obverses In J Prim. Inverse Prim. Inverse Prim. Inverse Prim. Inverse + self-inverse %: *: m&%: ^&m ^.&n %:&n - ^ ^. m&^ m&^. ,&n }: -. ^. ^ m&^. m&^ {&n n&i. % ,: {. m&|. (-m)&|. j.&n -&(j. n) %. ;: }:@;@(,&' '&.>"1) m&|: (] |:~ m C.^:_1 i.@#@\$) p..&n p.. |. #. #: m&, }. r.&n %&(2 1 o. n) |: #: #. m&#: m&#. +~ -: /: \: /:@|. m&{ not defined *~ %: [ {. ,: m&}. ({.~ -@(m&+)@#) ,~ <.@-:@# {. ] ] ". ": m&A. (/:@(m&A.)@(i.@#) { ]) ,:~ {. C. ": ". m&C. (/:@(m&C.)@(i.@#) { ]) ;~ >@{. p. j. 0j_1&* m&i. {&m j.~ 0.5j_0.5&* < > o. 1p_1&* m&j. 0j_1&*@(-&m) */ q: <: >: q: */ m&p.. p.. =/\ (= |.!.1) > < r. %&0j1@^. m&r. %&0j1@^.@(%&m) +/\ (- |.!.0) >: <: u: 3&u: +&n (-n)&+ */\ (% |.!.1) +. j./"1"_ x: _1&x: *&n (%n)&* -/\ ((- |.!.0) *"_1 \$&1 _1@#) +: -: m&+ (-m)&+ -&n n&+ %/\ ((% |.!.1) ^"_1 \$&1 _1@#) *. r./"1"_ m&* (%m)&* %&n n&* ~:/\ (~: |.!.0) *: %: m&- m&- %:&n ^.&n ({ =) Boolean perm. matrix i."1&1 integer perm vector -: +: m&% m&% ^&n n&%: I. (+/ @:(=/ i.@>:@(>./)@(0&,))) ^~ 3 : '(--&b@(*^.)%>:@^.)^:_]1>.b=.^.y'"0 !&n 3 : '(-(!&n-y"_)%0.001&*!&n"0 D:1])^:_[1>.{.@/:"1|y-/(i.!])n' ! 3 : '(-(!-y"_)%0.001&*!"0 D:1])^:_<.&170^:(-:+)^.y' m&#. (\$&m@>:@(m&(<.@^.))@(1&>.)@(>./)@:|@, #: ]) m&! 3 : '(-(m&!-y"_)%0.001&*m&!"0 D:1])^:_[m&<@|@{:}m,:m%:y*!m' p: The number of primes less than y

### Obverses Of Combinations

If any obverses are defined for u and v, they will be used to define obverses for u@v, u@:v, u&v, and u&:v.

The inverse of u"n is defined as (u^:_1)"n, which may not give desired results if the result cells of u do not have the same rank as u.

### Special Obverses

Some of the obverses are among the gems of J. They can be accessed indirectly through u&.v, where they apply v's obverse after u; or directly using ^:_1. What follows is a catalog of operations that are easy to express as obverses, difficult otherwise. It's not complete! If you find yourself needing, say, the inverse gamma function, see if !^:_1 does what you want.

#### Spaces Between Words ;:^:_1

If y is a list of boxed words, put the words into a sentence with ;:^:_1

```   ]a =: 'Three';'rather-short';'words'
+-----+------------+-----+
|Three|rather-short|words|
+-----+------------+-----+
;:^:_1 a
Three rather-short words
```

#### Convert to fixed base x #.^:_1

x #: y has the quirk that x specifies exactly how many places of the representation of y will be retained. Often you want as many repetitions of base x as necessary, no more or less. That's a job for #.^:_1

```   5 #: 127         NB. This gives only 1 place...
2
5 #.^:_1 (127)   NB. ... but this uses as many places as needed
1 0 0 2
```

Note that #.^:_1 is one of those obverses that has a dyadic valence. The inverse of the dyad cannot be calculated by #. b. _1, which gives the inverse of the monad.

#### Expand #^:_1

```   1 0 1 #^:_1 'ab'
a b
```

In the example above, 1 0 1 # 'a b' would select from 'a b' to produce 'ab'. #^:_1 does the reverse: it inserts fill into y to expand it so that the items of y correspond to 1s in x. For this to be possible, x must be Boolean and there must be exactly as many 1s in x as there are items in y. The fill can be set by !.f

```   1 0 1 0 1 #^:_1 'ab'        NB. Can't expand 2 items to 3 1s...
|length error
|   1 0 1 0 1    #^:_1'ab'
1 0 1 0 1 #^:_1 'abc'       NB. ...but 3 items is OK
a b c
1 0 1 0 1 #^:_1!.'*' 'abc'  NB. Fill can be specified
a*b*c
```

Note that #^:_1 is one of those obverses that has a dyadic valence. The inverse of the dyad cannot be calculated by # b. _1, which gives the inverse of the monad.

Quirk: x must be a list (a single atom is not allowed).

#### Primes Below y p:^:_1

The yth prime is p: y, so the number of primes below y (known to mathematicians as π(y)) is p:^:_1 y

```   p:^:_1 (60)
17
```

#### Frequencies of Integers I.^:_1

The obverse of I. is generalized to accept any nonnegative integral y, and returns the result as if y were sorted into ascending order. The atom with index i gives the number of occurrences of i in y.

```   I.^:_1 ]  3 1 4 1 5 9
0 2 0 1 1 1 0 0 0 1
```

## Oddities

1.  u b. _1 gives the obverse for the monadic valence.

In some cases the obverse supports the dyadic valence using a different verb. There is no general way to see what this different verb is, but you may be able to get a clue by seeing what the inverse of m&v or u&n is.

```   + b. _1          NB. The declared obverse of + is +...
+
4 +^:_1 (6)      NB. But it doesn't apply to the dyad
2
4&+ b. _1        NB. The dyadic obverse actually does this:
_4&+
```

```   #. b. _1         NB. #: is the obverse, but only for the monad:
#:
4 #: 66          NB. different...
2
4 (#.^:_1) ] 66  NB. ... results for dyad
1 0 0 2
4&#. b. _1   NB. The dyadic obverse actually looks like this:
(\$&4@>:@(4&(<.@^.))@(1&>.)@(>./)@:|@, #: ]) :.(4&#.)
```

2. The obverse of an obverse is always the original function, regardless of how the intermediate obverses are defined.