Replacement for @raoulb pr#914

See #914

ToDo:
- [x] tests
- [x] final rebase
There are even more cases, f.e:
```
In [92]: cos(7*pi/12)
Out[92]: -sqrt(-sqrt(3)/4 + 1/2)
```