What programming language are you using? This is difficult to answer because this is math question and I don't think Stack Overflow is the best platform to ask this question.
I think its best you post it here: Math Version of Stack Overflow