Why is it not always possible to compute the centroid of feature vectors?

Hi in the data mining and machine learning course that I’m taking there is a subject on feature spaces and there is this part about feature vector aggregation and metric spaces that I don’t really understand. Now our curriculum is basically a huge presentation and the whole information about feature vector aggregation fits on one slide, anyway what I don’t understand is this:

For a given sample $ D$ the centroid can be computed as $ C_D = \frac{1}{|D|}\cdot\sum_{o\in D} o$

and then it says: “In a general metric space (that is, not a vector space), where we only have pairwise distances, it might not be possible to compute a centroid”

I don’t really think I understand what “only pairwise distance” means (my guess is it has something to do with the fact that a metric space defines the distances between all pairs of elements in its set) or why the consequence of it is that you can’t always compute the centroid.

I read somewhere that metric spaces does not have to define addition or scaling and if that is the case it makes sense to me that you can not calculate the centroid. Is this what it essentially is about or am I completely misunderstanding something?

How to compute within a Multi tape turing machine [a binary substraction between 2 numbers and result on 3rd tape]?

I’m trying to compute a Multi-tape turing machine (of 3 tapes) that follows this statement.

“A binary subtraction operation between the first and second tape printing the result on the third tape”

I’ve been trying to do it in JFlap but I reached a halt point being pretty confused in this topic overall.

Any help regarding this MTTM would be really appreciated.

Compute manhattan distance from origin given a set of left-right-step directions

I am solving the problems for Advent of Code 2016 to learn Rust programming. The prompt for the first problem can be summarized:

I start at position 0,0 on a regular grid. I am given a set of directions to get to a location. I can only travel in “steps” on this grid, and I am only given directions in the form (e.g.):

“R5, L2, L1, R1, R3, R3, L3, R3, R4, L2, R4, L4, R4”

Where the first character is the direction to turn right or left and the following number is the number of steps to take. I need to compute the Manhattan distance between my starting point and the ending point.

The instructions are saved in a text file called “2016-1.txt”.

use std::fs;  struct Pos {     facing: char,     x: i32,     y: i32, }  fn split_dir(dir_str: &str) -> Vec<&str> {     dir_str.split(", ").collect() }  fn update_facing(rel_dir: &char, face_char: &char) -> char {     if *rel_dir == 'L' {         match face_char {             'N' => 'W',             'S' => 'E',             'E' => 'N',             'W' => 'S',             _ => 'I', // Is there a better way to handle the catch-all?         }     } else {         match face_char {             'N' => 'E',             'S' => 'W',             'E' => 'S',             'W' => 'N',             _ => 'I',         }     } }  fn update_x(pos_x: i32, face_char: char, move_num: i32) -> i32 {     match face_char {         'E' => pos_x + move_num,         'W' => pos_x - move_num,         _   => pos_x     } }  fn update_y(pos_y: i32, face_char: char, move_num: &i32) -> i32 {     match face_char {         'N' => pos_y + move_num,         'S' => pos_y - move_num,         _   => pos_y     } }  fn get_manhattan_dist(pos_x: i32, pos_y: i32, origin_x: i32, origin_y: i32) -> i32 {     (pos_x - origin_x).abs() + (pos_y - origin_y).abs() }  fn main() {     let s = fs::read_to_string("2016-1.txt")         .expect("Failed to read file.");      let split: Vec<&str> = split_dir(&s);      let mut pos: Pos = Pos {x: 0, y: 0, facing: 'N'};      for inst in split {         // Update direction         let rel_dir = inst.chars().nth(0).unwrap(); // Get first character of the instruction         pos.facing = update_facing(&rel_dir, &pos.facing);          // Update position         let move_num = &inst[1..].parse::<i32>().unwrap();         pos.x = update_x(pos.x, pos.facing, *move_num);         pos.y = update_y(pos.y, pos.facing, &move_num);     }      let dist = get_manhattan_dist(pos.x, pos.y, 0, 0);     println!("{}", dist); } 

I am particularly interested in error handling. Particularly in the “update_facing” function. This is my first Rust program, so all advice is warranted as well.

Compute binary sum with Python 3

I tried to make a function able to compute the binary sum. Binary numbers are passed as list composed of 1 and 0, like [1,1,0,1], [0,0,1], etc. It seems to work. I tried to improve the code, but I don’t think this is the best way to make it work. How can I improve this?

def check(value):     if value%2 == 0: #check if the number can be divided by 2         amount = int(value / 2) #if so, set the value to 0 and the amount as the number divided by 2         value = 0     elif value%2 != 0: #if not so, set the value to 0 and the amount as the previous even number divided by 2         amount = int((value-1)/2)         value = 1     return value, amount #returns the 2 value.  def binary_sum(*args): """The program compute the binary sum of 2+ binary lists, like [1,1,1,1] + [0,0,0,1]"""     i = -1 #control variable     result = [] #list that will be filled by results' binary numbers     value = 0      amount = 0 #amount carried over     binaryLength = [] #list of all args length     for x in args:  binaryLength.append(len(x)) #get the length of each args elements     maxLength = max(binaryLength) #get the max length among args elements     for x in args:         if len(set(binaryLength)) != 1: #checks if all elements have the same length             delta = maxLength - len(x) - 1 #if not so, it adds as many zeros as delta value indicates             while (delta) >= 0:                 x[:0] = [0]                 delta -= 1     while i >= -maxLength: #starts the sum from the end.         for j in args: value += j[i] #get the sum of all the last list element( for each arg)         value += amount          value, amount = check(value) #uses binary sum properties: it returns a value between 0 and 1, and the amount, an integer          result.insert(i, value) #inserts the value at the beginning of the list         i-=1 # needed to iterate the process         value = 0 #resets the value     if amount != 0: #At the end, if there's amount it inserts it at the beginning of result list:          while amount not in [0,1]: #if is not equal to 0 or 1 converts the int amount in binary number              value = amount             value, amount = check(value)             result.insert(i, value)             i-=1         result.insert(i, amount) #if is equal to 1 inserts it at the start of the list.     return result #returns the result as list. 

Python – Paradigm to compute different formulas with the same function

I have different equations to compute the same quantity. For example, val = my_func(a, b, c) but also val = my_func(x, y), where my_func represents the quantity I would like to compute: it could be Velocity or Momentum or something completely different. I have dozens of quantities to code, each with multiple equations.

It is also essential that I provide a decent documentation for the different input arguments.

EDIT: I believe the end users of the code will be smart enough to use some kind of advanced editor with autocompletion and capable of displaying docstrings.

So far, I have thought about three basic approaches.

Approach #1: I can write one function for each equation.
Pros: easy to write.
Cons: From a developer standpoint it may be difficult to maintain the code as it grows. From a user standpoint it becomes “cumbersome”, in the sense that the user is forced to look at the correct function that accepts its arguments.

def my_func_1(a, b, c):     """     Compute my_func.      Parameters     ----------         a : float             blablabla         b : float             blablabla         c : float             blablabla     """     return a + b + c  def my_func_2(x, y):     """     Compute my_func.      Parameters     ----------         x : float             blablabla         y : float             blablabla     """              return x * y 

Approach #2: I can write one function accepting keywords arguments and manage all the different cases!
Pros: Easy to use from a user standpoint (the user only need to read the documentation of one function and insert the correct parameters).
Cons: From a developer standpoint (in my opinion) it requires a little bit more attention to what you are coding. From a user standpoint, it becomes a little bit harder to use as he/she is forced to use the keyword approach, for example: my_func(x=my_x_val, y=my_y_val).

def my_func(**args):     """     Compute my_func. Formulas:         (1) val = my_func(a, b, c)         (2) val = my_func(x, y)      Please, provide input parameters accordingly to the equation      you desire to solve.      Parameters     ----------         a : float             blablabla         b : float             blablabla         c : float             blablabla         x : float             blablabla         y : float             blablabla     """      if all(k in args for k in ["a", "b", "c"]):         a, b, c = args["a"], args["b"], args["c"]         return a + b + c     if all(k in args for k in ["x", "y"]):         x, y = args["x"], args["y"]         return x * y      raise ValueError("The given arguments can't be used to compute the " +          "my_func. Please, read the function docs.") 

Approach #3: I can write one function accepting a index argument associated to the equation the user would like to solve. This function return the correct equation (function) and the user insert the different arguments.
Pros: Easy to use from a user standpoint as there is no need for keywords arguments! For example, my_val = my_func(2)(my_x_val, my_y_val).
Cons: From a user standpoint it would be a good idea to also have a copy of the arguments description in the parent function docstring, but this would insert a lot of repetition in the comments, hence increase difficulty in maintaining the code.

def my_func(idx):     """     Compute my_func. Formulas:         (1) val = my_func(1)(a, b, c)         (2) val = my_func(2)(x, y)      Please, insert the index of the formulas you would like to solve.     """      if idx == 1:         def func(a, b, c):             """             Parameters             ----------                 a : float                     blablabla                 b : float                     blablabla                 c : float                     blablabla             """             return a + b + c         return func     if idx == 2:         def func(x, y):             """             Parameters             ----------                 x : float                     blablabla                 y : float                     blablabla             """             return x * y         return func      raise ValueError("The given index can't be used to compute the " +          "my_func. Please, read the function docs.") 

As of now, I would be inclined to use approach #2.

Is there any better paradigm for this problem? Do you happen to know some code that faced this problem?

Compute the $g_Y$-parallel part of a $(0,1)$-form

Let $ Y$ be a compact (without boundary) Calabi-Yau manifold, i.e., $ c_1(Y)=0$ in $ H^2(Y, \mathbb{R})$ . Let $ \omega$ be a Kähler form on $ \mathbb{C}^m \times Y$ and let $ \omega_P = \omega_{\mathbb{C}^m} + \omega_Y$ . Assume that $ \zeta = \omega Р\omega_P$ is an exact $ (1,1)$ -form, i.e., $ \zeta = d\xi$ for some real $ 1$ -form $ \xi$ on $ \mathbb{C}^m \times Y$ . By the Leray spectral sequence of the projection $ \pi_{\mathbb{C}^m} : \mathbb{C}^m \times Y \to \mathbb{C}^m$ , there is an isomorphism $ \Phi : H^{0,1}(\mathbb{C}^m \times Y) \to \mathcal{O}(\mathbb{C}^m, H^{0,1}(Y))$ with $ $ \Phi[\xi^{0,1}](z) = [\xi^{0,1} \vert_{\{ z \} \times Y}].$ $ Set $ \Phi[\xi^{0,1}]=f$ and identify $ H^{0,1}(Y)$ with the space of $ g_Y$ -parallel $ (0,1)$ -forms.

Q: I want to show that $ $ \frac{\partial f }{\partial \overline{z}^j}=0.$ $

Moreover, I want to show that $ \dfrac{\partial f}{\partial z^j}$ is the $ H^{0,1}$ -class, or the $ g_Y$ -parallel part of the $ (0,1)$ -form $ $ (\partial_{z^j} \llcorner \zeta) \vert_{\{ z \} \times Y},$ $ where $ \llcorner$ denotes the interior product.

Algorithm to compute sum of all unique edge pairs of a tree

Given tree is undirected graph. It has n vertices and n-1 edges. The algorithm should compute the sum of all edge pairs. Thus, there are total nC2 or n(n-1)/2 such pairs. The time complexity of the mentioned algorithm is n(n-1)/2. Please suggest an algorithm with better space and time complexity if possible. Below is the java implementation.

import java.util.*;  public class AllPairSumTree {      static long sumAllPairs = 0;      public static void main(String[] args) {         /*          * Total Number of vertices          */         int N = 7;         /*          * Adjacency List          */         LinkedList<Integer>[] adjacencyList = new LinkedList[N];         /*          * Initialize Adjacency List          */         for(int ii=0; ii<N;ii++) {             adjacencyList[ii] = new LinkedList<Integer>();         }         /*          * Weighted Graph Matrix          */         int[][] weightedGraph = new int[N][N];         /*          * Initialize Matrix          */         for(int ii=0;ii<N;ii++) {             for(int jj=0;jj<N;jj++) {                 if(ii == jj) {                     weightedGraph[ii][jj] = 0;                 }else {                     weightedGraph[ii][jj] = Integer.MAX_VALUE;                 }             }         }          /*          * Input Pattern: vertex1,  vertex2, cost          *           * Total Vertex: N, Total Edges: N-1 (Tree, Undirected Graph)          */         int[] inputGraph = { 1, 2, 1,                                 2, 3, 2,                                 3, 4, 3,                                 3, 5, 4,                                 5, 6, 6,                                 5, 7, 5};          /*          * Assign  Adjacency List  and Matrix with input Graph          */         for(int ii=0; ii<N-1; ii++) {             int vertex1 = inputGraph[ii*3 + 0] - 1;             int vertex2 = inputGraph[ii*3 + 1] - 1;             int cost = inputGraph[ii*3 + 2];              adjacencyList[vertex1].add(vertex2);             adjacencyList[vertex2].add(vertex1);    //bidirectional edge              weightedGraph[vertex1][vertex2] = cost;             weightedGraph[vertex2][vertex1] = cost; //bidirectional edge         }          sumAllPairs = 0;         int currentVertex = 0;         LinkedHashSet<Integer> visitedSet = new LinkedHashSet<Integer>(N);         int lastVisitedVertex = -1;         allPairSum(weightedGraph, adjacencyList, currentVertex, visitedSet, lastVisitedVertex);          System.out.println(sumAllPairs);     }      /*      * Say graph has vertices 1,2,3,4,5,6,7      *       * allPairSum() will compute sum of edges like this:      *              21 + (31+32) + (41+42+43) + (51+52+53+54) + (61+62+63+64+65)+(71+72+73+74+75+76)      *              where ij represents edge from vertex i to vertex j      *       * Time Complexity:      *      N(N-1)/2 or Combination(N,2)[![enter image description here][1]][1]      */     private static void allPairSum(int[][] weightedGraph, LinkedList<Integer>[] adjacencyList, int currentVertex, LinkedHashSet<Integer> visitedSet, int lastVisitedVertex) {          for(Integer visitedVer : visitedSet) {             int cost = weightedGraph[visitedVer][lastVisitedVertex] +  weightedGraph[lastVisitedVertex][currentVertex];             sumAllPairs += cost;             weightedGraph[visitedVer][currentVertex] = cost;             weightedGraph[currentVertex][visitedVer] = cost;         }          visitedSet.add(currentVertex);          for(Integer neighbourVert : adjacencyList[currentVertex]) {             if(neighbourVert != lastVisitedVertex) {                 /*                  *      neighbourVert becomes currentVertex                  *      currentVertex becomes lastVisitedVertex                  */                 allPairSum(weightedGraph, adjacencyList, neighbourVert, visitedSet, currentVertex);             }         }     } } 

Sample tree with 7 vertices and 6 edges

Is there an algorithm to compute a Belyi map for the Riemann surface?

Let $ y^2=x^5-x-1$ be an affine model of a projective complex curve, is there an algorithm to compute the Belyi map (preferably of small degree), i.e., map to the projective line ramified only at $ \{0,1,\infty\}$ .

In my attempts to do this by hand, I get ramification at 4 points and, subsequently, using Shabat polynomial will skyrocket the degree of the map. Is there a way to avoid increasing the degree to thousands or hundreds? I have $ \beta=h\circ g\ \circ f$ , where $ f$ is projection on $ x$ , $ g=x^5-x-1$ and $ h=\frac{( 12500 x + 18750 x^2 + 12500 x^3 + 3125 x^4)}{-2869}$ . $ \beta$ gives ramification at four points $ \{0,1, \infty, \frac{3125}{2869}\}$ . Also, it is possible to use $ g=x^5-x$ with different $ h$ , but the Shabat polynomial will still be of very big degree….