In the canonical or NVT ensemble the number of particles, volume, and temperature are fixed. We have microstates  and macrostates  and a coarse-graining function . Each microstate has an energy defined by . In the canonical ensemble the Hemholtz free energy defined to be:
where
When assuming entropy is maximized it can be derived that the probability of being in a microstate is
And it can be found that
where the partition function is
For brevity, because  are all fixed for a certain ensemble, we omit them from the function arguments.
Taking the gradient of  gives:
Then substituting  in for its definition gives
And the only part of the expression that depends on  is the delta function.
For any function  so 
The gradient can be converted from  to  using a matrix  that is constrained by the Jacobian of .
Converting the gradient with the pseudoinverse of the Jacobian
The gradient with respect to  is
And since the expression  only depends on  through :
meaning
and is equivelent to
where  is the Jacobian of  at .
Starting with
For any matrix  such that
Will satisfy
Showing the pseudoinverse is a possible solution if 
The transposed pseudinverse of the Jacobian,  is one possible solution for , but is not necessarily the only solution.
If we multiply both sides by 
Now if we assume that  is invertable, which it is if the rows of  are linearely independent (this assumption implies  must be true placing additional constraints on the choice of coarse graining function), then we can multiply both sides by its inverse.
The expression on the right is simply the formula for the pseudoinverse of a matrix, (if it has more columns than rows which is true for  because ).
so substituting 
And the pseudoinverse and transpose are commutative so we will just write  as 
So after replacing the gradient we get the expression
Next, using the divergence theorem where the total divergence of a volume  is equal to the surface integral . If  is a scalar function and  is a vector function then
Where  is the normal vector to the surface.
The product rule for divergence is
And when applied to the divergence theorem gives
which is a generalization to integration by parts to vector fields
 has dimension  so we can treat each row as a vector. In the following notation we apply dot products and divergences to each row of , so  means dot producting each row which is equivelent to matrix muliplication. We substitute  and  into the divergence theorem.
When  the boundary points  will contain points on the edge of the volume. Any points not inside the volume should have an infinite energy so that   should be zero (not sure about this part about eliminating the boundary term, could be wrong). The the entire left hand side of the equality should be zero.
so then
and dot product-ing each row of a matrix is just matrix multiplication so
Then substitue into the original equation gives
Using the divergence product rule again
Giving the final equation
Seperating energy and entropic terms
Energy term
We want to seperate the Helmholtz free energy into energy and entropic terms.
If we leave out the fixed terms  we can write this as
We start with the definition of 
Take the gradient
Then apply the product rule
The product rule creates 2 terms we must calculate. Starting with the first term.
Gradient of the first term
Applying the chain rule
p
And the integral is just the internal energy by definition
Now we just need to find . Expanding its definition:
Converting the gradients again using the Jacobian:
And applying the same reasoning as the previous section using the divergence theorem and the divergence product rule:
Then the divergence product rule again
Then the chain rule
This equation gives an expression for . Substituting gives
This equation gives an expression for the first term.
Next we have to find the gradient of the second term.
Gradient of the second term
Starting with the term
we can move the gradient to the only part that depends on  
Once again convert the gradient
Once again use the divergence theorem and the divergence product rule.
Then the divergence product rule again
Use the product rule
Then the chain rule
Factor out the exponential
This equation gives an expression for the second term.
Now we just add the expressions for the 2 terms together
We can group some terms
So we have a final expression of
Entropic term
Starting with the definition of entropy
Using basic algebraic manupulation
So this is consistent that
Then
and we can substitute
and one of the terms cancels out with a final expression of
Isothermal-Isobaric Ensemble (not sure about this part)
We must first derive some properties about the Isothermal-Isobaric ensemble, or NPT ensemble. Starting with the NVT partition function, where  is the set of all points in the volume
And if the heat bath is considered to be an ideal gas then
which is equivelent to the total volume to the power of the number of particles
If we assume the particles and the heat bath don't share volume then
Using the natural gas law
We can find a formula for the number of particles in the bath
and substituting
In the limit that  and approximating 
So we get an expression for the partition function of the heat bath:
The combined partition function is
Consider the set  of possible points is parameterized by some variables , for example the lengths of a box. Also consider a coarse graining function on the parameters of the volume  where the coarse grained parameters are 
Compared to the canonical ensemble the partition function is .
And it can be shown that the Gibbs free energy
And for brevity will be written as
and where the microstates are  and there are 2 mapping function . The Gibbs free energy can be found to be:
Then to calculate the gradient
Chain rule
Put in the definition
Move the graident inside
We can make  a function of 
take the gradient of boths sides
chain rule
then algebraic manipulation
substituting for  gives
cancel out the denominator
and this is simply the weighted average based off the parameters of the volume, giving a final expression of
Move gradient
We can also calculate the macrostate "pressure"
Chain rule
Put in the definition
and using the divergence theorem again with the argument the surface integral is zero because the pressure of the bath as the volume of the system becomes infinitely big becomes infinitely large as the system takes up all the volume
Using the same substitution for  gives
chain rule
p
Giving a final expression of
Seperating energy, entropic, and pressure contribution
TODO