# Porting a CAS to Julia

**URL:** https://discourse.julialang.org/t/porting-a-cas-to-julia/9924
**Category:** General Usage
**Created:** [March 20, 2018, 5:01pm UTC](https://discourse.julialang.org/t/porting-a-cas-to-julia/9924 "2018-03-20T17:01:21Z")
**Posts on this page:** 2
**Page:** 2

<div class="post-metadata">

### Author: ![o314](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/o314/32/252_2.png) [@o314](https://discourse.julialang.org/u/o314)
#### Post date: [April 10, 2018, 1:55pm UTC](https://discourse.julialang.org/t/porting-a-cas-to-julia/9924/21 "2018-04-10T13:55:10Z")

</div>

(Replying to myself)

I repost there a comment i made on the page [Rock–paper–scissors game in less than 10 lines of code](https://giordano.github.io/blog/2017-11-03-rock-paper-scissors)

It seems to me that Julia type system built around algebraic data type and multidispatch can be well described using computational topology principle in particular cochain complexes, [cech cohomology](https://en.wikipedia.org/wiki/%C4%8Cech_cohomology), may be [topological data analysis](https://en.wikipedia.org/wiki/Topological_data_analysis)

I particulary like the work of Robert Ghrist and his recent book [Ghrist; 2014; Elementary Applied Topology](https://en.wikipedia.org/wiki/Special:BookSources?isbn=1502880857)  
that i am still studying. Here is an excerpt (p117)

![0bdf4006cf31a753cf5169040609ceeb7d5c75afacf98bcde1fa68b525fd688c](https://global.discourse-cdn.com/julialang/original/3X/9/7/97a27601d481088fd3df89e585bb9b469dea7ef3.jpeg)

IMHO, that’s why OOP fails so often: jailing every method in one class and trying to serve everyone at 360 degrees. Served with pointing, pivoting, nesting again and again. Epic failure about to happen.

That would be nice if this could be better explained and may be even represented and included into a Julia CAS (which will lead to a kind of self-hosting computational and mathematical language)

---

<div class="post-metadata">

### Author: ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)
#### Post date: [April 24, 2018, 1:29am UTC](https://discourse.julialang.org/t/porting-a-cas-to-julia/9924/22 "2018-04-24T01:29:17Z")

</div>

> [@Elrod](#):
>
> One of my motivations in this is realizing how slow the AD packages I’ve tried are at finding Hessians with respect to x of the form:
> 
> ```Julia
> f(x,A) = x' * A * x / 2
> 
> ```
> 
> where the answer should of course simply be “return A”. Instead of being a noop, they’re pretty slow. A symbolic approach should be able to simplify things and get there quite easily. I can’t help but think packages like XGrad would be easier to write if they could use a CAS. Perhaps also combining with DataFlow (as a better @fastmath) to eliminate redundant code for getting all the derivatives 0,…,k.

On the current `master` branch of `Reduce` along with the unregistered package `ReduceLinAlg`, which provides the `hessian` constructor, you can now do something like this along with other things.

```Julia
julia> using Reduce, ReduceLinAlg
Reduce (Free PSL version, revision 4068), 16-Jun-2017 ...

julia> v = [:w, :x, :y, :z]
4-element Array{Symbol,1}:
 :w
 :x
 :y
 :z

julia> A = hessian(:x * :y * :z + :x ^ 2, v) |> mat
4×4 Array{Any,2}:
 0 0 0 0  
 0 2 :z :y
 0 :z 0 :x
 0 :y :x 0  

julia> transpose(v) * A * v / 2
:((x + 3 * y * z) * x)

```

However, note that if you use the `v'` operation for transpose, the conjuage is used, so

```Julia
julia> v*v'
4×4 Array{Expr,2}:
 :((repart(w) - impart(w) * im) * w) … :((repart(z) - impart(z) * im) * w)
 :((repart(w) - impart(w) * im) * x) :((repart(z) - impart(z) * im) * x)
 :((repart(w) - impart(w) * im) * y) :((repart(z) - impart(z) * im) * y)
 :((repart(w) - impart(w) * im) * z) :((repart(z) - impart(z) * im) * z)

```

Additionally, there are many other built-in functions that can work with symbolic AST expressions

```Julia
julia> det(A+7*I)
:(-7 * (7 * ((z ^ 2 - 63) + y ^ 2) + (9x - 2 * y * z) * x))

```

The core features of those have been implemented, although there still may be some edge cases where it doesn’t work properly. The following is the new ReduceLinAlg package, which gives an initial external demonstration of how the parser generator can be used to extend the Julia language:

> <https://github.com/JuliaReducePkg/ReduceLinAlg.jl/blob/master/src/ReduceLinAlg.jl>

Other developers can certainly try to add more functions to the `ReduceLinAlg` repository by using the `parsegen` function provided by Reduce.jl and by studying the [upstream docs](http://www.reduce-algebra.com/manual/manualse127.html). More explanatations will be laid out on the documentation in the near future, to help developers understand the process of extending Julia using the Reduce parser.

[Previous page](https://discourse.julialang.org/t/porting-a-cas-to-julia/9924.md?page=1)
