Not sure of the rest of the world, but at least in the US, patenting “software” is a pretty murky subject legally (at least it feels that way when trying to do some basic research on it) Something that seems common among sources discussing it is that “Software Related Inventions” (eg, a computer that does XYZ) can be patentable, but software/code itself is not literally patentable. Seemingly, because we’re talking about libraries that would be pure software, not a product for sale based on it, you wouldn’t be able patent libraries like you’re talking about.
I’d provide links to some discourse of this, but honestly I think it’s better to search “can you patent software in the US” and do a brief read of various sources, because the terminology between them can seem somewhat counterfactual to eachother.