you should pre-calculate martix once