A numerical method for evaluating matrix elements of hermitian operators