General algorithm for manipulating nonlinear and linear entanglement witnesses by using exact convex optimization