Most efficient way of writing specific quadratic form