This question is an extension to the question I asked previously and referred to in the link above. I want to add spin to my system, so I changed it to the following code. the problem is that although I am using Compile, it is still very slow. I didn't change it much, so I don't understand why it is becoming so slow?

This is the code after including the spin by using the function kdfxn which acts as a delta function. gives 1 in case I have equal spin for instance if sz1 and sz2 are equal and gives 0 otherwise. The only changed I made is in chmat, the rest is the same as previous.

$\begingroup$@b3m2a1 Ok, Thank you. Sorry for the Junk!!! I am beginner, maybe I couldn't find a way to make it shorter. Anyway, Thanks for your help$\endgroup$
– Delaram NematollahiOct 23 '17 at 21:05

$\begingroup$"junk" was too harsh a word. I just meant it's a lot. Generally we advocate breaking things down into the most minimal piece you can find. In this case that might be using a chmat adapted so that it used a smaller cfxn that had fewer parameters being fed into it. That would also have helped you find the issue, which was the kdfxn.$\endgroup$
– b3m2a1Oct 23 '17 at 21:07

$\begingroup$yeah I need to get used it more, I have already read, seems didn't get it completely.$\endgroup$
– Delaram NematollahiOct 23 '17 at 21:07

1

$\begingroup$@DelaramNematollahi understandable. It's a very different way to think about the code. Just keep in mind that DownValues can't be compiled and you'll be much better off.$\endgroup$
– b3m2a1Oct 23 '17 at 21:08

Mathematica is a registered trademark of Wolfram Research, Inc. While the mark is used herein with the limited permission of Wolfram Research, Stack Exchange and this site disclaim all affiliation therewith.