{"id":17090844,"url":"https://github.com/phimage/arithmosophi","last_synced_at":"2025-08-06T13:12:43.345Z","repository":{"id":33938144,"uuid":"37662536","full_name":"phimage/Arithmosophi","owner":"phimage","description":"A set of protocols for Arithmetic, Statistics and Logical operations","archived":false,"fork":false,"pushed_at":"2021-01-26T23:41:34.000Z","size":1198,"stargazers_count":67,"open_issues_count":0,"forks_count":12,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-12T06:03:21.486Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/phimage.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-06-18T13:56:43.000Z","updated_at":"2024-07-04T11:07:05.000Z","dependencies_parsed_at":"2022-07-13T16:48:12.385Z","dependency_job_id":null,"html_url":"https://github.com/phimage/Arithmosophi","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phimage%2FArithmosophi","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phimage%2FArithmosophi/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phimage%2FArithmosophi/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phimage%2FArithmosophi/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/phimage","download_url":"https://codeload.github.com/phimage/Arithmosophi/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248639411,"owners_count":21137840,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-14T13:56:53.249Z","updated_at":"2025-04-12T22:29:20.713Z","avatar_url":"https://github.com/phimage.png","language":"Swift","readme":"# Arithmosophi - Arithmosoϕ\n\n[![Join the chat at https://gitter.im/phimage/Arithmosophi](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/phimage/Arithmosophi?utm_source=badge\u0026utm_medium=badge\u0026utm_campaign=pr-badge\u0026utm_content=badge)\n[![License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat\n            )](http://mit-license.org)\n[![Platform](http://img.shields.io/badge/platform-ios_osx-tvos-lightgrey.svg?style=flat\n             )](https://developer.apple.com/resources/)\n[![Language](http://img.shields.io/badge/language-swift-orange.svg?style=flat\n             )](https://developer.apple.com/swift)\n[![Issues](https://img.shields.io/github/issues/phimage/Arithmosophi.svg?style=flat\n           )](https://github.com/phimage/Arithmosophi/issues)\n[![Cocoapod](http://img.shields.io/cocoapods/v/Arithmosophi.svg?style=flat)](http://cocoadocs.org/docsets/Arithmosophi/)\n[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)\n\n[\u003cimg align=\"left\" src=\"logo.png\" hspace=\"20\"\u003e](#logo) Arithmosophi is a set of missing protocols that simplify arithmetic and statistics on generic objects or functions.\nAs `Equatable` define the `==` operator , `Addable` will define the `+` operator.\n```swift\nprotocol Addable {\n    func + (lhs: Self, rhs: Self) -\u003e Self\n}\n[1, 2, 3, 4].sum //  1 + 2 + 3 + 4\n[0, 1, 2, 3, 4].average // 2\n[13, 2.4, 3, 4].varianceSample\n```\n\n- As you might guess `Substractable` define `-` operator, `Multiplicatable` define `*` operator, etc..., all defined in [Arithmosophi.swift](Sources/Arithmosophi.swift)\n\n## Contents ##\n- [Generic functions](#generic-functions)\n- [CollectionType](#collectiontype)\n  - [Average](#average)\n  - [Median](#median)\n  - [Variance](#variance)\n  - [Standard deviation](#standard-deviation)\n  - [Skewness](#skewness)\n  - [Kurtosis](#kurtosis)\n  - [Covariance](#covariance)\n- [Object attributes](#object-attributes)\n- [Logical operations](#logical-operations)\n- [Complex](#complex)\n- [Geometry](#geometry)\n- [Setup](#setup)\n\n## Generic functions ##\nTake a look at `sumOf` function\n```swift\nfunc sumOf\u003cT where T:Addable, T:Initializable\u003e(input : [T]) -\u003e T {\n    return reduce(input, T()) {$0 + $1}\n}\n```\nArray of `Int`, `Double` and even `String` could be passed as argument to this function. Any `Addable` objects.\n\nNo need to implement a function for `Double`, one for `Float`, one more for `Int`, etc...\n\n*`sumOf` and `productOf` functions are available in [Arithmosophi.swift](Arithmosophi.swift)*\n\n\n## CollectionType ##\nThis framework contains some useful extensions on `CollectionType`\n```swift\n[1, 2, 3, 4].sum //  1 + 2 + 3 + 4\n[1, 2, 3, 4].product //  1 * 2 * 3 * 4\n\n[\"a\",\"b\",\"c\",\"d\"].sum // \"abcd\" same as joinWithSeparator(\"\")\n[[\"a\",\"b\"],[\"c\"],[\"d\"]].sum // [\"a\",\"b\",\"c\",\"d\"] same as flatMap{$0}\n```\n\n### Average ###\n_with [MesosOros.swift](Sources/MesosOros.swift)_\n\nComputes [arithmetic average/mean](http://en.wikipedia.org/wiki/Arithmetic_mean)\n```swift\n[1, 2, 3, 4].average //  (1 + 2 + 3 + 4) / 4\n```\n\nA type is `Averagable` if it can be dividable by an `Int` and define an operator to do that\n```swift\nfunc /(lhs: Self, rhs: Int) -\u003e Self\n```\nAll arithmetic type conform to this protocol and you can get an average for a `CollectionType`\n\nP.S. You can conform to this protocol and `Addable` to make a custom average.\n\n### Median ###\n_with [MesosOros.swift](Sources/MesosOros.swift)_\n\nGet the [median value](http://en.wikipedia.org/wiki/Median) from the array\n\n- Returns the average of the two middle values if there is an even number of elements in the `CollectionType`.\n```swift\n[1, 11, 19, 4, -7].median // 4\n```\n- Returns the lower of the two middle values if there is an even number of elements in the `CollectionType`.\n```swift\n[1.0, 11, 19.5, 4, 12, -7].medianLow // 4\n```\n- Returns the higher of the two middle values if there is an even number of elements in the `CollectionType`.\n```swift\n[1, 11, 19, 4, 12, -7].medianHigh // 11\n```\n\n### Variance ###\n_with [Sigma.swift](Sources/Sigma.swift)_\n\nComputes [variance](http://en.wikipedia.org/wiki/Variance).\n\n- [The sample variance](https://en.wikipedia.org/wiki/Variance#Sample_variance): Σ( (Element - average)^2 ) / (count - 1)\n\n```swift\n[1.0, 11, 19.5, 4, 12, -7].varianceSample\n```\n- [The population variance](https://en.wikipedia.org/wiki/Variance#Sample_variance): Σ( (Element - average)^2 ) / count\n\n```swift\n[1.0, 11, 19.5, 4, 12, -7].variancePopulation\n```\n\n### Standard deviation ###\n_with [Sigma.swift](Sources/Sigma.swift)_\n\nComputes [standard deviation](http://en.wikipedia.org/wiki/Standard_deviation).\n\n- [sample based](https://en.wikipedia.org/wiki/Standard_deviation#Sample-based_statistics)\n```swift\n[1.0, 11, 19.5, 4, 12, -7].standardDeviationSample\n```\n- [population based](https://en.wikipedia.org/wiki/Standard_deviation#Population-based_statistics)\n```swift\n[[1.0, 11, 19.5, 4, 12, -7].standardDeviationPopulation\n```\n\n### Skewness ###\n_with [Sigma.swift](Sources/Sigma.swift)_\n\nComputes [skewness](https://en.wikipedia.org/wiki/Skewness).\n\n```swift\n[1.0, 11, 19.5, 4, 12, -7].skewness // or .moment.skewness\n```\n\n### Kurtosis ###\n_with [Sigma.swift](Sources/Sigma.swift)_\n\nComputes [kurtosis](https://en.wikipedia.org/wiki/Kurtosis).\n\n```swift\n[1.0, 11, 19.5, 4, 12, -7].kurtosis // or .moment.kurtosis\n```\n\n### Covariance ###\n_with [Sigma.swift](Sources/Sigma.swift)_\n\nComputes [covariance](http://en.wikipedia.org/wiki/Covariance) with another `CollectionType`\n\n- [sample covariance](http://en.wikipedia.org/wiki/Sample_mean_and_sample_covariance)\n```swift\n[1, 2, 3.5, 3.7, 8, 12].covarianceSample([0.5, 1, 2.1, 3.4, 3.4, 4])\n```\n- population covariance\n```swift\n[1, 2, 3.5, 3.7, 8, 12].covariancePopulation([0.5, 1, 2.1, 3.4, 3.4, 4])\n```\n\n- [Pearson product-moment correlation coefficient](http://en.wikipedia.org/wiki/Pearson_product-moment_correlation_coefficient)\n```swift\n[1, 2, 3.5, 3.7, 8, 12].pearson([0.5, 1, 2.1, 3.4, 3.4, 4])\n```\n\n## Complex ##\n_with [Complex.swift](Sources/Complex.swift)_\n`Complex` is a struct of two `ArithmeticType`, the real and the imaginary component\n\n```swift\nvar complex = Complex(real: 12, imaginary: 9)\ncomplex = 12 + 9.i\n```\nYou can apply operation on it `(+, -, *, /, ++, --, -)`\n\n```\nresult = complex + 8 // Complex(real: 20, imaginary: 9)\n\nComplex(real: 12, imaginary: 9) + Complex(real: 8, imaginary: 1)\n // Complex(real: 20, imaginary: 10)\n```\n\n## Object attributes ##\nThe power of this simple arithmetic protocols are released when using operators\n\nIf we implement a box object containing a generic `T` value\n```swift\nclass Box\u003cT\u003e {\n\tvar value: T\n}\n```\nwe can define some operators on it, in a generic way, like we can do with `Equatable` or `Comparable`\n```swift\nfunc +=\u003cT where T:Addable\u003e (inout box: Box\u003cT\u003e, addend: T) {\n    box.value = box.value + addend\n}\nfunc -=\u003cT where T:Substractable\u003e (inout box: Box\u003cT\u003e, addend: T) {\n    box.value = box.value - addend\n}\n```\nhow to use this operator:\n```swift\nvar myInt: Box\u003cInt\u003e(5)\nmyInt += 37\n```\n\nFor a full example, see [Prephirence](https://github.com/phimage/Prephirences/blob/master/Prephirences/Preference.swift) file from [Prephirences](https://github.com/phimage/Prephirences) framework, or sample [Box.swift](Samples/Box.swift)\n\n### Optional trick ###\nFor optional attribute you can use `Initializable` or any protocol which define a way to get a value\n```swift\nclass Box\u003cT\u003e {\n\tvar value: T?\n}\nfunc +=\u003cT where T:Addable, T:Initializable\u003e (inout box: Box\u003cT\u003e, addend: T) {\n    box.value = (box.value ?? T()) + addend\n}\n```\n\n## Logical operations  ##\n[`LogicalOperationsType`](Sources/LogicalOperationsType.swift) is a missing protocol for `Bool` inspired from `BitwiseOperationsType` (or `IntegerArithmeticType`)\n\nThe purpose is the same, implement functions without knowing the base type\n\nYou can for instance implement your own [`Boolean` enum](Samples/Boolean.swift) and implement the protocol\n```swift\nenum Boolean: LogicalOperationsType {case True, False}\nfunc \u0026\u0026 (left: Boolean, @autoclosure right:  () -\u003e Boolean) -\u003e Boolean {\n    switch left {\n    case .False: return .False\n    case .True:  return right()\n    }\n}\n...\n```\nthen create only **one** operator on `Box` for `Bool`, `Boolean` and any `LogicalOperationsType`\n```swift\nfunc \u0026\u0026=\u003cT:LogicalOperationsType\u003e (inout box: Box\u003cT\u003e, @autoclosure right:  () -\u003e TT) {\n    box.value = box.value \u0026\u0026 right()\n}\n```\n\nTake a look at a more complex enum [Optional](Samples/Optional.swift) which implement also `LogicalOperationsType`\n\n## Geometry ##\nwith `Arithmos`(number) \u0026 `Statheros`(constant)\n\n[`Arithmos`](Sources/Arithmos.swift) and [`Statheros`](Sources/Statheros.swift) add respectively functions and  mathematical constants for `Double`, `Float` and `CGFloat`, allowing to implement generic functions without taking care of type\n\n```swift\nfunc distance\u003cT: Arithmos\u003e(#x: T, y: T) -\u003e T {\n\treturn x.hypot(y)\n}\n\nfunc radiansFromDegrees\u003cT where T: Multiplicable, Dividable, T: Arithmos, T: Statheros\u003e(degrees: T) -\u003e T {\n\treturn degrees * T.PI / T(180.0)\n}\n```\n\nTake a look at [Geometry.swift](Samples/Geometry.swift) for more examples\n\n## Setup\n### Using [cocoapods](http://cocoapods.org/) ##\n```ruby\npod 'Arithmosophi'\n```\nNot interested in full framework ? install a subset with:\n```ruby\npod 'Arithmosophi/Core' # Arithmosophi.swift\npod 'Arithmosophi/Logical' # LogicalOperationsType.swift\npod 'Arithmosophi/Complex' # Complex.swift\npod 'Arithmosophi/MesosOros' # MesosOros.swift\npod 'Arithmosophi/Arithmos' # Arithmos.swift\npod 'Arithmosophi/Sigma' # Sigma.swift\npod 'Arithmosophi/Statheros' # Statheros.swift\n\npod 'Arithmosophi/Samples' # Samples/*.swift (not installed by default)\n```\n\n*Add `use_frameworks!` to the end of the `Podfile`.*\n\n#### Make your own framework dependent\nIn podspec file\n```ruby\ns.dependency 'Arithmosophi'\n```\nor define only wanted targets\n```ruby\ns.dependency 'Arithmosophi/Core'\ns.dependency 'Arithmosophi/Logical'\n```\n\n## Using xcode ##\nDrag files to your projects\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphimage%2Farithmosophi","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fphimage%2Farithmosophi","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphimage%2Farithmosophi/lists"}